Which method is executed first in Java?
Which method is executed first in Java?
In Java, the order of execution for different methods can be a bit tricky to understand, especially for beginners. Let's dive into it!
Initialization Methods
When a Java program starts executing, several initialization methods are called before any other method is executed. The order in which these methods are called is as follows:
static initializer methods: These are static block-level initializations that are executed only once when the class is first loaded. instance initializer methods: These are instance-level initializations that are executed for each object created from the class.These initialization methods are responsible for setting up the class's internal state and preparing it for use.
Constructors
After the initialization methods have finished executing, constructors are called. In Java, a constructor is a special method that has the same name as the class and no return type (not even void). The purpose of a constructor is to initialize an object when it's created.
Main Method (Entry Point)
The main method is the entry point for any Java program. This is the method that the JVM (Java Virtual Machine) starts executing when you run your Java program.
So, to summarize:
Initialization methods (static initializer and instance initializer) Constructors Main method (entry point)Now, let's take a look at some code to illustrate this:
public class MyClass {
// Static initializer method
static {
System.out.println("Static initialization");
}
// Instance initializer method
{
System.out.println("Instance initialization");
}
public MyClass() {
System.out.println("Constructor called!");
}
public static void main(String[] args) {
System.out.println("Main method started!");
new MyClass();
}
}
If you run this program, the output will be:
"Static initialization" "Instance initialization" (multiple times) "Main method started!" "Constructor called!" (twice)So, in summary:
Initialization methods (static and instance) are executed first. Constructors are then called when objects are created. Finally, the main method is executed as the entry point for your program.I hope this helps!
How main method is executed in Java?
In Java, the main method is the entry point of a program, where execution begins. It's the starting point from which your program executes. The main method is declared with the following signature:
public static void main(String[] args) {
// program code goes here
}
Here's how the main method is executed in Java:
JVM Initialization: When a Java program starts, the Java Virtual Machine (JVM) initializes itself by creating several essential components such as the heap memory area for storing objects and an execution stack. Main Method Search: The JVM looks for themain
method by searching for a class with a main
method in the following places: In the public class
that is currently executing. In the classes of any superclasses or interfaces. In the classes of any libraries or frameworks (like Spring, Hibernate) being used. Class Loading: If the JVM finds the main class, it loads its definition into memory. This process involves reading the .class
file from the file system or a network connection, and then creating an instance of the class in memory. Main Method Invocation: The JVM invokes the main
method by creating a new thread for execution. In this thread, the JVM sets up the args
array passed to the main method, which is used to store command-line arguments or other parameters that were passed when the program was launched. Program Execution: Once the main
method is invoked and its body starts executing, your Java program begins running. The code within this method is executed, creating objects, calling methods on those objects, performing I/O operations (like reading files or printing output), and so forth, just like any other Java method. JVM Shutdown: When the main
method finishes execution or an exception occurs that cannot be caught and processed further, the JVM begins to shut down. This involves releasing system resources, such as closing open file handles, sockets, or network connections. Program Exit: Finally, when all necessary shutdown operations have been performed, the JVM terminates its execution, and your Java program exits.
The main method plays a crucial role in initializing and launching the entire Java application process. It provides a centralized entry point for all programs that you write using this popular programming language.