Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, September 12, 2023

Top 15 Java ClassLoader Interview Questions and Answers for Experienced Programmers

Hello guys, if you are preparing for Java interviews and want to refresh your knowledge of Classloaders in Java then you have come to the right place. Earlier, I have shared Java interview questions on Collections, Multithreading, Serialization, and Generics, and in this article, I am going to share frequently asked Java interview questions about class loaders. If you don't know, a class loader is a program that loads classes in Java programs. The mechanism of class loading is very important to understand how the Java program works. When you run a Java program you give the name of the main class, from their onwards, built-in classloaders like Bootstrap classloader start loading not just the main classes but other Java library classes which is required to run that program. 

Three main class loaders are used by JVM to load all the classes required to execute a Java program, they are, Bootstrap class loader, Extension class loader, and System or Application class loader. Except for the BootStrap ClassLoader, all ClassLoader is implemented in Java and extends java.lang.ClassLoader, which is an abstract class in Java.

I have explained this topic in detail in my earlier post about How Classloader works in Java, If you are new to classloader then I suggest you read that article to understand the fundamental concepts of Classloader and their working mechanism

In this article also, I have included both basic and advanced class loader questions so that you can get decent ideas about how a class loader works in a core Java application and in a managed Java EE environment when web servers and application servers are involved. 

By the way, if you are serious about your Java interview then I also suggest you check out Java Interview Guide: 200+ Interview Questions and Answers course on Udemy. This course is created by Ranga Karnam of In28Minutes and it has a good collection of questions for Java interviews. You can use this course to quickly cover all essential Java concepts. 




15 Advanced Core Java ClassLoader Interview Questions and Answers

Here is my list of some of the frequently asked class loader-based questions from Java Interviews. This list is not complete but contains some of the interesting questions and highlights some essential concepts related to both class loading in Java and class loaders.


1. What is ClassLoader in Java? From where does they load classes? and Where class related meta data are stored?
ClassLoader in Java is a class that is used to load other classes in Java virtual machines. This is the most frequently asked interview question about ClassLoader in Java. There are primarily three class loaders that are used by JVM bootstrap class loader, extension class loader, and System or application class loader. 

Except for the Bootstrap class loader, all ClassLoader is implemented in Java and extends java.lang.ClassLoader, which is an abstract class in Java. Also all the class related meta data like class definition, fields and methods are stored in METASPACE part of JVM memory. 

Every ClassLoaders loads .class file from a particular location, BootStrap Classloader is responsible for loading System classes and loads the class file from rt.jar, extension class loader loads class from JRE/lib/ext directory, and Application ClassLoader loads class from the CLASSPATH environment variable, -classpath or -cp option while running Java application or from Class-path attribute of Manifest file of the JAR file in Java.

What is ClassLoader in Java? From where does they load classes? and Where class related meta data are stored?




2. How ClassLoader works in Java?
This is another Java ClassLoader interview question that is getting popular, which is very good because a correct understanding of How ClassLoader works in Java is essential for any serious Java programmer. 

ClassLoader works on the Delegation principle, where a request to load a class is a delegate to the parent class loader before any attempt to load the class.  If a parent doesn't find the class in its specified location like CLASSPATH, the current Classloader searches for that class on its designated source and loads that Class.

Another important principle of class loading in Java is visibility, A class loaded by a parent is visible to All its child Classloaders but any class loaded by child ClassLoader is not visible to any parent ClassLoader and an attempt to load a particular class which is only visible to Child will result in java.lang.NoClassDefFoundError in Java, one of the common Java errors related to class loading. 

You can also see the Understanding the Java Virtual Machine: Class Loading and Reflection course by Kevin Jones on Pluralsight to learn more about class loading in Java. This is a great course to learn class loader delegation, how to use URL ClassLoader, create your own classloader, etc. 

Top 10 Java ClassLoader Interview Questions and Answers


By the way, you would need a Pluralsight membership to join this course which costs around $29 per month or $299 per year (14% discount). I highly recommend this subscription to all programmers as it provides instant access to more than 7000+ online courses to learn any tech skill. Alternatively, you can also use their 10-day-free-trial to watch this course for FREE.



3. What are the two rules of ClassLoader in Java?
As mentioned in the previous Java ClassLoader interview question, two rules of ClassLoader in Java are delegation and visibility. Delegation takes delegates class loading request to parent class loader before trying, According to visibility principle all class loaded by parent ClassLoader is visible to child classloader.


4. Can you implement your own ClassLoader in Java?
Yes, you can implement your own ClassLoader in Java which can load .class files or any binary which can be translated or mapped into a .class file from any source like a database or network. You need to extend java.lang.ClassLoader which is an abstract class and needs to override the findClass() method, which takes delegation.

Can you implement your own ClassLoader in Java?




5. Can the same class be loaded by multiple classloaders in Java?
Yes, the Same class can be loaded by multiple ClassLoader in Java. A class is uniquely identified by Java virtual machine by not only its fully qualified name but also in the context of ClassLoader. This is a very useful feature of Java ClassLoader which treats class loaded from a different source even though they share name, very much a possibility on Applets where classes are normally downloaded from different sources.


6. What does System.getClass().getClassLoader() returns?
The getClassLoader() method of java.lang.The class returns the class loader for the class, I mean, the name of ClassLoader which loaded that particular class.

System classes like String or any other class from JDK are loaded by the bootstrap class loader and they are implemented in the native language mostly in C and don't have the corresponding java.lang.ClassLoader instance. So this call will return null.
System.out.println("System.getClass().getClassLoader() : " 
                   + System.class.getClassLoader());
Output : System.getClass().getClassLoader() : null


7. Can you load a class that is not in CLASSPATH?
Yes, it's possible to load classes that are not in Classpath. Since Java Virtual Machine itself uses three different ClassLoader: BootStrap, extension, and System or Application class loaders, out of that only Application ClassLoader uses CLASSPATH to search classes, Extension ClassLoader loaded a class from jre/lib/ext directory.

So if you put any Class in this directory it will be loaded by Extension ClassLoader. you can even define your own ClassLoader which can load classes from a network, database, or any other source. Though CLASSPATH is the most common place where Java searches for classes it's not the only place.


8. What is extension ClassLoader in Java? from where does it load classes?
Extension class loader is one of three default class loader existed in the Java programming language. Extension class loader is a child of BootStrap ClassLoader and loads classes from the jre/lib/ext directory.

Some time programmer put class files in the jre/lib/ext directory, To avoid setting Java classpath which works but it can create subtle issues like if you put a newer version of Class files on Classpath and expecting your Java program to pick it from Classpath, either forgetting or not realizing that those classes already exist in jre/lib/ext directory.

What is class loaders in Java





9. What is the binary name of Class in Java?
What do you think? Can you answer this question? If you know just answer in the comments and I will put the first 3 people's names here, who answered it correctly. 


10. What is the Context ClassLoader in Java?
Same here, can you try to answer these questions without doing Google? If you can, just answer and I will put the first 3 people's names here, who answer this question correctly on the comments Here is also a nice diagram which explains classloading process in Java:

What is the Context ClassLoader in Java?




11. Can two objects load from different ClassLoader can be equal in Java?
No, two objects loaded from different ClassLoader can not be the same. In the run time a class is not only recognized by its fully qualified name but also in the context of ClassLoader. 

Since the same class can be loaded by multiple ClassLoader which is quite possible in J2EE environments which have package hierarchies and distribute the code in form of multiple JARS like WAR and EJB-JAR.

If you remember correctly How to override the equals method in Java, you will see that the equals method has a check for getClass() which will return false if it's loaded by two different ClassLoaders, if you don't have that check on equals() method then it will throw ClassCastException while comparing two classes loaded by different class loaders.



12. Can Parent ClassLoader see classes loaded by Child ClassLoader in Java?
No, the Parent ClassLoader can not see classes loaded by the child ClassLoader in Java. As explained in a first ClassLoader interview question, Java Classloader works on the delegation principle where a request to load a class is delegated to the parent ClassLoader.

But if the parent doesn't found the class and ultimately the child loads that class, it will not be visible to the parent ClassLoader, and any attempt for loading that class from the parent ClassLoader will result in NoClassDefFoundError in Java.

Can Parent ClassLoader see classes loaded by Child ClassLoader in Java?




13. If you call Class.forName() then which ClassLoader will load that particular class?
This is a very interesting ClassLoader interview question in Java. Class.forName() is an overloaded method in Java and there are two versions of Class.forName() exists, where the overloaded version also accepts a ClassLoader instance which will be used to load and initialize the class. 

A Call to Class.forName("binary name of Class) is equivalent to Class.forName(className, true, currentLoader), which means current ClassLoader will be used to load that class if its not loaded already.



14. Does all the classes are loaded by classloader at the start?
No, not all the classes are loaded by classloaders from start, only thos which are needed are loaded. They follow lazy loading and only load the class when a static variable or method is invoked. You can also load a class using the Class.forName() method which is often used to load JDBC driver class from respective JAR Files. 


That's all about some of the important questions related to class loaders in Java. ClassLoader is a tricky concept to master and not every Java developer pays attention to how Classworks. You can use these interview questions to not only refresh your Classloader knowledge in Java but also prepare for Java interviews better. 

Further Learning
Understanding the Java Virtual Machine: Class Loading and Reflection
     

Other Java Interview Question Articles You may like 

Thanks for reading this article so far. If you like these advanced classloader Java interview questions for experienced developers then please share with your friends and colleagues. If you have any feedback or doubt then please drop a note   

1 comment :

Anonymous said...

Hello Javin, which book would you recommend to read about ClassLoader in Java? I want to master this topic

Post a Comment