• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding Execution of Java class  RSS feed

 
santhosh kumar vk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

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?


Thanks,
Santhosh Kumar V.K
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • 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.
 
pardeepkumar devgon
Greenhorn
Posts: 8
Java Oracle
  • Mark post as helpful
  • send pies
  • 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.



 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!