Search code examples
javaobjectclassloader

Java application startup - how first object is created


When we run a Java application, we know that the first object to be loaded is java.lang.Object. However, java.lang.Object has methods which throw exceptions like CloneNotSupportedException or InterruptedException, which are in turn other objects.

The question is: when java.lang.Object is just getting loaded, how is it possible to have its child (for example exception objects) objects being already created?


Solution

  • loaded and instantiatedare two different things.

    Rough explanation:

    loaded means the JVM loaded a class into its base class loader. This makes the class available for instantiation. When JVM starts, it first loads all classes through the used/linked jar files, without instantiating them. This means, when the first new Object() is created, the exceptions which are used by Object are already known. This is still a declaration. The instance of the exception is created only when that particular exception is thrown.

    For technically correct explanation see the link which @Jim Garrison has mentioned:

    JVM: Loading, Linking, and Initializing