Class loaders are hierarchical. Classes are introduced into the JVM as they are referenced by name in a class that
is already running in the JVM. So how is the very first class loaded? The very first class is specially loaded with
the help of static main() method declared in your class. All the subsequently loaded classes are loaded by the
classes, which are already loaded and running. A class loader creates a namespace. All JVMs include at least one
class loader that is embedded within the JVM called the primordial (or bootstrap) class loader. Now let's look at
non-primordial class loaders. The JVM has hooks in it to allow user defined class loaders to be used in place of
primordial class loader. Let us look at the class loaders created by the JVM.
Class Loader | Reloadable | Explanation |
Bootstrap(Primordial) | No | Loads JDK internal classes, java.* packages. (as defined system property, typically loads rt.jar and i18n.jar) |
Extensions | No | Loads jar files from JDK extensions directory (as defined in the java.ext.dirs system property – usually lib/ext directory of the JRE) |
System | No | Loads classes from system classpath (as defined by the java.class.path property, which is set by the CLASSPATH environment variable or –classpath or –cp command line options) |
Class loaders are hierarchical and use a delegation model when loading a class. Class loaders request their
parent to load the class first before attempting to load it themselves. When a class loader loads a class, the child
class loaders in the hierarchy will never reload the class again. Hence uniqueness is maintained. Classes loaded
by a child class loader have visibility into classes loaded by its parents up the hierarchy but the reverse is not true
as explained in the above diagram.
Important: Two objects loaded by different class loaders are never equal even if they carry the same values, which mean a
class is uniquely identified in the context of the associated class loader. This applies to singletons too, where each class
loader will have its own singleton.
Explain static vs. dynamic class loading?
Static Class Loading | Dynamic Class Loading |
Classes are statically loaded with Java's "new" operator. class MyClass { public static void main(String args[]) { Car c = new Car(); } } | Dynamic loading is a technique for programmatically invoking the functions of a class loader at run time. Let us look at how to load classes dynamically. Class.forName (String className); //static method which returns a Class The above static method returns the class object associated with the class name. The string className can be supplied dynamically at run time. Unlike the static loading, the dynamic loading will decide whether to load the class Car or the class Jeep at runtime based on a properties file and/or other runtime conditions. Once the class is dynamically loaded the following method returns an instance of the loaded class. It's just like creating a class object with no arguments. class.newInstance (); //A non-static method, which creates an instance of a class (i.e. creates an object). Jeep myJeep = null ; //myClassName should be read from a properties file or Constants interface. //stay away from hard coding values in your program. String myClassName = "au.com.Jeep" ; Class vehicleClass = Class.forName(myClassName) ; myJeep = (Jeep) vehicleClass.newInstance(); myJeep.setFuelCapacity(50); |
A NoClassDefFoundException is thrown if a class is referenced with Java's "new" operator (i.e. static loading) but the runtime system cannot find the referenced class. | A ClassNotFoundException is thrown when an application tries to load in a class through its string name using the following methods but no definition for the class with the specified name could be found: -The forName(..) method in class - Class. -The findSystemClass(..) method in class - ClassLoader. - The loadClass(..) method in class - ClassLoader. |
What are "static initializers" or "static blocks with no function names"? When a class is loaded, all blocks
that are declared static and don't have function name (i.e. static initializers) are executed even before the
constructors are executed. As the name suggests they are typically used to initialize static fields.
public class StaticInitilaizer {
public static final int A = 5;
public static final int B;
//Static initializer block, which is executed only once when the class is loaded.
static {
if(A == 5)
B = 10;
else
B = 5;
}
public StaticInitilaizer(){} // constructor is called only after static initializer block
}
The following code gives an Output of A=5, B=10.
public class Test {
System.out.println("A =" + StaticInitilaizer.A + ", B =" + StaticInitilaizer.B);
}
Thank you for the good writeup. It in reality was once
ReplyDeletea leisure account it. Look advanced to far brought agreeable from you!
However, how can we keep in touch?
Also visit my web-site: free email html templates
Hi! I'm at work surfing around your blog from my new iphone! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the superb work!
ReplyDeleteHere is my webpage - signature email
Appreciate this post. Will try it out.
ReplyDeleteMy webpage ... email templates outlook
certainly like your web site but you need to test the spelling on several of
ReplyDeleteyour posts. Many of them are rife with spelling issues and
I in finding it very troublesome to inform the truth however I'll certainly come back again.
My webpage email advertising templates
Just wish to say your article is as surprising.
ReplyDeleteThe clearness in your post is simply great and i
could assume you are an expert on this subject. Fine with your permission let me to grab your RSS feed to keep up to date with forthcoming post.
Thanks a million and please keep up the rewarding work.
Look at my web blog: email marketing software free
Great blog! Is your theme custom made or did
ReplyDeleteyou download it from somewhere? A theme like yours with a few simple adjustements would really make my
blog stand out. Please let me know where you got your theme.
Thanks
Also visit my web site :: riveting
What's up friends, fastidious piece of writing and fastidious arguments commented here, I am in fact enjoying by these.
ReplyDeleteHere is my webpage - just click the following website
balenciaga trainers
ReplyDeletegoyard
balenciaga shoes
adidas tubular
hermes
mbt shoes
ferragamo belts
curry 4
coach outlet online
hermes belt
essayez ceci meilleures répliques de sacs de créateurs allez vers ces gars dolabuy.co vous pouvez essayer ici https://www.dolabuy.su
ReplyDeletee0b58r5n86 t8a38n8r23 l5u13u1m65 l5g93k1e37 z4n71f4s97 s6y51i5a43
ReplyDelete