Class
1 | public abstract class ClassLoader { |
Most important method is
loadClass(arg)
,
it accept a class’s full name, and return it’s instance.defineClass(arg)
accept a byte array, turn it into instance.
Usually, it load a file from disk, then pass file byte into JVM.
Instantiate Class into a instance through JVM’s(native method) define to Class.getResource()
andgetResources
search URL from repository.
They have agent mechanism as well as loadClass, like:defineClass(getResource(name).getBytes())
Code
Because Java late binding and explain character,
Classes’ will be load when it needed like
static method/ construct method or direct allocate in field.
1 | public class A { |
They are same when B
have default constructor.1
2
3
4B b = new B();
B b = Class.forName("B", false,
A.class.getClassLoader()).newInstance();