Home » Java programming language

Is main() method compulsory in Java?

Here, we are going to learn about the main() method in Java: Is main() method compulsory in Java? Can we write a java program without main() method?
Submitted by Preeti Jain, on July 16, 2019

The question is that "Is main() method is compulsory in Java?"

  • Yes, we can write a java program without main() method but there is a condition if and only if java JDK version till JDK 5.
  • Till Java JDK 5 main() method was not compulsory to include in Java program.
  • If we don't write our code in the main() method or don't include main() method in our program then, in that case, we need to write our code under static block then only, in that case, we can execute our code normally as we do.

Example:

// Java Program to demonstrate till Java JDK5 version 
// without main() method is possible.
class WithoutMainMethod {
    static {
        int i = 2, j = 4, sum;
        
        sum = i + j;
        
        System.out.println("The sum of i and j is :" + sum);
        System.out.println("This program is without main() valid till JDK 5 version");
    }
}

Output

E:\Programs>javac WithoutMainMethod.java

E:\Programs>java WithoutMainMethod
The sum of i and j is : 6
This program is without main() valid till JDK 5 version
  • In the case of the static block is that static block executes before the main() method.
  • Static block executes at the time of class loading.
  • In the case of the main() method, our program starts executing from the main() method or in other words it is the starting point of the program execution.
  • We can call the main() method directly without the creation of an object because it is static.
  • Till Java JDK 5 main() method was not mandated, But from Java JDK 6 main() is mandatory and if we don't include main() method in our program then we will get RuntimeException "main method not found in the class".

Example:

// Program to demonstrate without main() method 
// from Java JDK 6 version
class WithoutMain{
	int i=2 , j=4 , sum=0;
	sum = i + j;
	System.out.println("The sum of i and j is :" + sum);
	System.out.println("This program without main() is not valid from JDK 6 version");
}

Output

E:\Programs>javac WithoutMain.java

E:\Programs>java WithoutMain
Error: Main method not found in class WithoutMain, please define the main method as:
   public static void main(String[] args)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.