• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Regarding Execution of Java class

Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Could you please tell me the flow when the class is run by jvm, Which class loader will load all the classes at run time and which method will call first and when static method will be called?

Santhosh Kumar V.K
Java Cowboy
Posts: 16084
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The JRE (Java runtime environment) will first load and initialize the class that you specified on the command line. It will then call the public static void main(String[] args) method in that class.

Classes are loaded only when needed. Whenever the program uses a class for the first time, it's loaded and initialized.

Static methods are called only when the program calls them. The JRE isn't going to call any static methods automatically (besides the main method). If you mean static initializers (instead of static methods), they are called to initialize the class, immediately after the class is loaded.
Posts: 8
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When the JVM is started, three class loaders are used
Bootstrap class loader
Extensions class loader
System class loader

The bootstrap class loader loads the core Java libraries[5] located in the <JAVA_HOME>/jre/lib directory. This class loader, which is part of the core JVM, is written in native code.
The extensions class loader loads the code in the extensions directories (<JAVA_HOME>/jre/lib/ext,[6] or any other directory specified by the java.ext.dirs system property).
The system class loader loads code found on java.class.path, which maps to the system CLASSPATH variable.

In JVM, each and every class is loaded by some instanc of java.lang.classloader and its located in java.lang package and we are free to extend to add own functionality to class loading.

Whenever a new JVM started by typing "java MyClass", 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.

Next come java extension class Loader, responsible for loading extension libraries means jar file which provide features that go beyond the core java, in the path given by the java.ext.dirs property.
The Extension Class Loader is responisble for loading all the jar files kept in java.ext.dirs path.
Developer can add his or her application jar or whatever libraries he/she might need to add to the classpath to this extension directly so the Extension Class Loader will load.

The third and foremost class loader from developer perspective is the AppClassLoader or System Class Loader responisble for loading all the classes kept in the path corresponding to java.class.path system property

jvm loads class
executes static blocks
looks for main method and invokes it
So, if there's code in a static block, it will be executed.

    Bookmark Topic Watch Topic
  • New Topic