Three major class loaders are utilized by JVM to load all of the lessons required to execute a Java program, they’re, Bootstrap class loader, Extension class loader, and System or Software class loader. Aside from the BootStrap ClassLoader, all ClassLoader is applied in Java and extends java.lang.ClassLoader, which is an summary class in Java.
On this article additionally, I’ve included each fundamental and superior class loader questions so to get first rate concepts about how a category loader works in a core Java utility and in a managed Java EE setting when internet servers and utility servers are concerned.
15 Superior Java ClassLoader Interview Questions and Solutions
Right here is my checklist of among the incessantly requested class loader-based questions from Java Interviews. This checklist isn’t full however comprises among the attention-grabbing questions and highlights some important ideas associated to each class loading in Java and sophistication loaders.
1. What’s ClassLoader in Java? From the place does they load lessons? and The place class associated meta knowledge are saved?
ClassLoader in Java is a category that’s used to load different lessons in Java digital machines. That is probably the most incessantly requested interview query about ClassLoader in Java. There are primarily three class loaders which can be utilized by JVM bootstrap class loader, extension class loader, and System or utility class loader.
Each ClassLoaders masses .class file from a specific location, BootStrap Classloader is accountable for loading System lessons and masses the category file from rt.jar, extension class loader masses class from JRE/lib/ext listing, and Software ClassLoader masses class from the CLASSPATH setting variable, -classpath or -cp choice whereas operating Java utility or from Class-path attribute of Manifest file of the JAR file in Java.
2. How ClassLoader works in Java?
That is one other Java ClassLoader interview query that’s getting fashionable, which is excellent as a result of an accurate understanding of How ClassLoader works in Java is crucial for any critical Java programmer.
One other necessary precept of sophistication loading in Java is visibility, A category loaded by a dad or mum is seen to All its baby Classloaders however any class loaded by baby ClassLoader isn’t seen to any dad or mum ClassLoader and an try and load a specific class which is barely seen to Youngster will end in java.lang.NoClassDefFoundError in Java, one of many frequent Java errors associated to class loading.
3. What are the 2 guidelines of ClassLoader in Java?
As talked about within the earlier Java ClassLoader interview query, two guidelines of ClassLoader in Java are delegation and visibility. Delegation takes delegates class loading request to dad or mum class loader earlier than making an attempt, In line with visibility precept all class loaded by dad or mum ClassLoader is seen to baby classloader.
4. Are you able to implement your personal ClassLoader in Java?
Sure, you possibly can implement your personal ClassLoader in Java which may load .class recordsdata or any binary which might be translated or mapped right into a .class file from any supply like a database or community. You must prolong java.lang.ClassLoader which is an summary class and must override the findClass() methodology, which takes delegation.
5. Can the identical class be loaded by a number of classloaders in Java?
Sure, the Identical class might be loaded by a number of ClassLoader in Java. A category is uniquely recognized by Java digital machine by not solely its absolutely certified identify but additionally within the context of ClassLoader. This can be a very helpful characteristic of Java ClassLoader which treats class loaded from a unique supply regardless that they share identify, very a lot a chance on Applets the place lessons are usually downloaded from totally different sources.
6. What does System.getClass().getClassLoader() returns?
The getClassLoader() methodology of java.lang.The category returns the category loader for the category, I imply, the identify of ClassLoader which loaded that specific class.
System lessons like String or some other class from JDK are loaded by the bootstrap class loader and they’re applied within the native language principally in C and haven’t got the corresponding java.lang.ClassLoader occasion. So this name will return null.
System.out.println("System.getClass().getClassLoader() : "
+ System.class.getClassLoader());
Output : System.getClass().getClassLoader() : null
7. Are you able to load a category that’s not in CLASSPATH?
Sure, it is doable to load lessons that aren’t in Classpath. Since Java Digital Machine itself makes use of three totally different ClassLoader: BootStrap, extension, and System or Software class loaders, out of that solely Software ClassLoader makes use of CLASSPATH to look lessons, Extension ClassLoader loaded a category from jre/lib/ext listing.
So in case you put any Class on this listing it will likely be loaded by Extension ClassLoader. you possibly can even outline your personal ClassLoader which may load lessons from a community, database, or some other supply. Although CLASSPATH is the commonest place the place Java searches for lessons it is not the one place.
8. What’s extension ClassLoader in Java? from the place does it load lessons?
Extension class loader is certainly one of three default class loader existed within the Java programming language. Extension class loader is a toddler of BootStrap ClassLoader and masses lessons from the jre/lib/ext listing.
A while programmer put class recordsdata within the jre/lib/ext listing, To keep away from setting Java classpath which works however it might create refined points like in case you put a more recent model of Class recordsdata on Classpath and anticipating your Java program to select it from Classpath, both forgetting or not realizing that these lessons exist already in jre/lib/ext listing.
9. What’s the binary identify of Class in Java?
What do you suppose? Are you able to reply this query? If you recognize simply reply within the feedback and I’ll put the primary 3 individuals’s names right here, who answered it appropriately.
10. What’s the Context ClassLoader in Java?
Identical right here, are you able to attempt to reply these questions with out doing Google? When you can, simply reply and I’ll put the primary 3 individuals’s names right here, who reply this query appropriately on the feedback Right here can also be a pleasant diagram which explains classloading course of in Java:
11. Can two objects load from totally different ClassLoader might be equal in Java?
No, two objects loaded from totally different ClassLoader cannot be the identical. Within the run time a category isn’t solely acknowledged by its absolutely certified identify but additionally within the context of ClassLoader.
When you bear in mind appropriately How you can override the equals methodology in Java, you will note that the equals methodology has a examine for getClass() which is able to return false if it is loaded by two totally different ClassLoaders, if you do not have that examine on equals() methodology then it’ll throw ClassCastException whereas evaluating two lessons loaded by totally different class loaders.
12. Can Dad or mum ClassLoader see lessons loaded by Youngster ClassLoader in Java?
No, the Dad or mum ClassLoader cannot see lessons loaded by the kid ClassLoader in Java. As defined in a primary ClassLoader interview query, Java Classloader works on the delegation precept the place a request to load a category is delegated to the dad or mum ClassLoader.
But when the dad or mum would not discovered the category and in the end the kid masses that class, it is not going to be seen to the dad or mum ClassLoader, and any try for loading that class from the dad or mum ClassLoader will end in NoClassDefFoundError in Java.
13. When you name Class.forName() then which ClassLoader will load that specific class?
This can be a very attention-grabbing ClassLoader interview query in Java. Class.forName() is an overloaded methodology in Java and there are two variations of Class.forName() exists, the place the overloaded model additionally accepts a ClassLoader occasion which shall be used to load and initialize the category.
A Name to Class.forName(“binary identify of Class) is equal to Class.forName(className, true, currentLoader), which implies present ClassLoader shall be used to load that class if its not loaded already.
14. Does all of the lessons are loaded by classloader at the beginning?
No, not all of the lessons are loaded by classloaders from begin, solely thos that are wanted are loaded. They comply with lazy loading and solely load the category when a static variable or methodology is invoked. You too can load a category utilizing the Class.forName() methodology which is commonly used to load JDBC driver class from respective JAR Information.
That is all about among the necessary questions associated to class loaders in Java. ClassLoader is a tough idea to grasp and never each Java developer pays consideration to how Classworks. You should utilize these interview inquiries to not solely refresh your Classloader data in Java but additionally put together for Java interviews higher.
Additional Studying
Different Java Interview Query Articles You could like
Thanks for studying this text thus far. When you like these superior classloader Java interview questions for knowledgeable builders then please share with
your folks and colleagues. If
you will have any suggestions or doubt then please drop a be aware