There is a class A, which calls a methods of class B.
Now, I am generating class B dynamically, and every time when new class B is generated, A should call the newer version of class B. Now, I am loading class B using custom class loader, but class B also gets loaded using default class loader from within class A.
So, the class loaded by my custom class loader becomes irrelevant. Class B must only get loaded using custom class loader because my need is: I want to reload the class B more than once.
How to proceed?
From where I should load the class B? Is it from within class A or even before class A gets loaded? Also, is it required to load class A using custom class loader too, as Class A is going to call methods of class B?
Make sure A is loaded through the same classloader that B is loaded from.
Unless you are only passing around primitives, you get ClassCastExceptions passing instances between classloaders.