Classloader: Instance
Classloader: Instance
In Java, you first write a .java file which is then compiled to .class
file.Java is capable of loading classes at run time.
The basic principle of java class loading is that classes are only
loaded when needed. Each class that gets loaded may have other classes
that it depends on, so the loading process is recursive.
A lot happens inside the JVM when a class is loaded and initialized,
Decoding the binary class format,
Checking compatibility with other classes,
Verifying the sequence of bytecode operations, and
Finally constructing a java.lang.Class instance to represent the
new class.
Class loader can load classes from one level above its hierarchy.First
level is bootstrap class loader, second level is extensions
classloader and third level is system class loader.All Java class
loaders except bootstrap, are implemented using java.lang.ClassLoader.
java.lang.ClassclassObj = ClassName.class;
When a class name is given, class loader first locates the class and
then reads a class file of that name from the native file system.
All class loaders except bootstrap class loader have a parent class
loader. This parent is not as in parent-child relationship of
inheritance. Every class loader instance is associated with a parent
class loader.
http://zeroturnaround.com/rebellabs/rebel-labs-tutorial-do-you-really-
get-classloaders/#!/
Tour of Java classloading mechanism, both from the JVM and developer
point-of-view.
Something goes wrong with classloading, how would you know to solve
it?
How and why classloaders leak and how can that be remedied.
We’ll see how objects, classes and classloaders are tied to each other
and the process required to make changes.
java.lang.ClassLoader
First, here is the API, with some less relevant parts omitted:
CustomClassLoading
A user class loader gets the chance to load a class before the
primordial class loader does. Because of this, it can load the class
implementation data from some alternate source.
Dynamic ClassLoading
http://tutorials.jenkov.com/java-reflection/dynamic-class-loading-reloading.html
Similar to Custom class loader, class can be loaded dynamically by
extending java.lang.ClassLoader subclass.
Challenges
The resolve() method will not allow any given ClassLoader instance to
link the same class twice. Therefore, everytime you want to reload a
class you must use a new instance of your ClassLoader subclass.