Static Block and main() Method in Java Last Updated : 23 Dec, 2024 Comments Improve Suggest changes Like Article Like Report In Java static block is used to initialize the static data members. Static block is executed before the main method at the time of class loading. Example: Java // static block is executed before main() class staticExample { // static block static { System.out.println("Inside Static Block."); } // main method public static void main(String args[]) { System.out.println("Inside main method."); } } OutputInside Static Block. Inside main method. Can we execute a Java class without declaring main() method?The answer is No. Since JDK 1.7 it is not possible to execute any java class without main() method. But it was one of the ways till JDK 1.6.Example: Java // without Main function but with static block // The below code would not work in JDK 1.7 class staticExample { // static block execution without // main method in JDK 1.6. static { System.out.println("Inside Static Block."); System.exit(0); } } Output: (In JDK 1.6) Inside Static Block.But from the above code gives an error in output. Output: (JDK 1.7 onwards)Error: Main method not found in class staticExample, please define the main method as: public static void main(String args[]) or a JavaFX application class must extend javafx.application.ApplicationStatic vs Instance Block in JavaSometimes the query occurs if we have static block in Java then what is instance block. The difference lies between the functionality of both the concepts:Static Block: Runs once when the class is loaded (before any object creation).Instance Block: Runs every time a new instance is created (before the constructor is called).Example: Java // static and instance block import java.io.*; class Geeks { // static Block static { System.out.println("Static Block Called"); } // main method public static void main (String[] args) { // Object Created Geeks obj = new Geeks(); System.out.println("Main Method"); } // instance block { System.out.println("Instance Block Called"); } } OutputStatic Block Called Instance Block Called Main Method Comment More infoAdvertise with us Next Article Static Block and main() Method in Java P pragati_mehrotra Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Java Methods Java Methods are blocks of code that perform a specific task. A method allows us to reuse code, improving both efficiency and organization. All methods in Java must belong to a class. Methods are similar to functions and expose the behavior of objects.Example: Java program to demonstrate how to crea 8 min read Parameter Passing Techniques in Java with Examples There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the "caller function" and B is called the "called function or callee function". Also, the arguments wh 4 min read Java is Strictly Pass by Value! In order to understand more of how the java is processing the parameter in methods and functions, lets compare the java program with a C++ code which would make it more clear and helps you get the major difference between how the parameters are being passed to any methods or functions wrt passing pa 6 min read How are parameters passed in Java? See this for detailed description. In Java, parameters are always passed by value. For example, following program prints i = 10, j = 20. java // Test.java public class Test { // swap() doesn't swap i and j public static void swap(Integer i, Integer j) { Integer temp = new Integer(i); i = j; j = temp 1 min read Method overloading and null error in Java In Java it is very common to overload methods. Below is an interesting Java program. Java public class Test { // Overloaded methods public void fun(Integer i) { System.out.println("fun(Integer ) "); } public void fun(String name) { System.out.println("fun(String ) &quo 2 min read Can we Overload or Override static methods in java ? Let us first define Overloading and Overriding. Overriding: Overriding is a feature of OOP languages like Java that is related to run-time polymorphism. A subclass (or derived class) provides a specific implementation of a method in the superclass (or base class). The implementation to be executed i 5 min read Access specifier of methods in interfaces In Java, all methods in an interface are public even if we do not specify public with method names. Also, data fields are public static final even if we do not mention it with fields names. Therefore, data fields must be initialized. Consider the following example, x is by default public static fina 1 min read Java main() Method - public static void main(String[] args) Java's main() method is the starting point from where the JVM starts the execution of a Java program. JVM will not execute the code if the program is missing the main method. Hence, it is one of the most important methods of Java, and having a proper understanding of it is very important.The Java co 6 min read Is main method compulsory in Java? The answer to this question depends on the version of java you are using. Prior to JDK 7, the main method was not mandatory in a java program. You could write your full code under static block and it ran normally. The static block is first executed as soon as the class is loaded before the main(); t 2 min read Understanding "static" in "public static void main" in Java Following points explain what is "static" in the main() method: main() method: The main() method, in Java, is the entry point for the JVM(Java Virtual Machine) into the java program. JVM launches the java program by invoking the main() method. Static is a keyword. The role of adding static before an 3 min read Like