Deepu--Java
Deepu--Java

Reputation: 3840

How does the Java class loading work

As I know that before loading of any class in Java Environment, one class should be running. That's why our main is static. But can anyone tell what mechanism works to get this first class running.?

Because it's origin of class loading and I never understood this phenomenon.

Thanks for any feedback.

Upvotes: 2

Views: 548

Answers (3)

Arnaud Denoyelle
Arnaud Denoyelle

Reputation: 31273

You understood the need of the bootstrap class loader

It will create the first classes that cannot be loaded by a class loader (because classes extend Object (including ClassLoader.class) and Object.class needs a class loader to be loaded...). The bootstrap class loader is written in native code. That is why it does not need a classLoader to be instantiated.

This is how the first classes will be created. Among them, the class "ClassLoader" which will then be able to instantiate the others.

btw, if you call getClassLoader() on a class which is loaded by the bootstrap class loader (i.e. System.class), you will get null.

Upvotes: 3

Aniket Thakur
Aniket Thakur

Reputation: 69005

It's the JVM that is responsible for loading all the classes. As all languages have rules , in Java public static void main(String args[]) method signature is used to start the program (your logical workflow). This does not mean JVM loads the class with main method and that class then loads other classes.

Docs clearly say

The Java Virtual Machine starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader (§5.3.1). The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main(String[]). The invocation of this method drives all further execution. Execution of the Java Virtual Machine instructions constituting the main method may cause linking (and consequently creation) of additional classes and interfaces, as well as invocation of additional methods.

In an implementation of the Java Virtual Machine, the initial class could be provided as a command line argument. Alternatively, the implementation could provide an initial class that sets up a class loader which in turn loads an application. Other choices of the initial class are possible so long as they are consistent with the specification given in the previous paragraph.

JVM loads an initial class that is implementation-dependent which then loads class loader which loads other classes.

Upvotes: -1

Deepanshu J bedi
Deepanshu J bedi

Reputation: 1540

In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The ClassLoader class is located in the java.lang package and developers are free to subclass it to add their own functionality to class loading.

In the Java runtime, each and every class will have its code also available in the form of a first-class Java object, which is an instance of java.lang.Class. Whenever we compile any Java file, the compiler will embed a public, static, final field named class, of the type java.lang.Class, in the emitted byte code. Since this field is public, we can access it using dotted notation, like this:

java.lang.Class klass = Myclass.class;

Once a class is loaded into a JVM, the same class (I repeat, the same class) will not be loaded again Whenever a new JVM is started by typing java MyMainClass, the "bootstrap class loader" is responsible for loading key Java classes like java.lang.Object and other runtime code into memory first. The runtime classes are packaged inside of the JRE\lib\rt.jar file. We cannot find the details of the bootstrap class loader in the Java documentation, since this is a native implementation. For the same reason, the behavior of the bootstrap class loader will also differ across JVMs. more about class loading class loading in short

Upvotes: 0

Related Questions