Is the main method compulsory in Java? - Programmer's Academy

Is the main method compulsory in Java?

Since the Java method is one of the important Java methods,   developers consider it compulsory in Java.  It is true to some extent, but not in all cases.

The main method is the entry point in Java and whenever we execute a program in Java JVM searches for the main method and begins to execute it.

Moreover, there is a condition that if you are using the Java JDK version till JDK 5,   you don’t need to use the main method in your program and more so, you should write your code under a static block then you can code normally.

For example: 

Here Code:

// 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");
}
}

Here is the output of the above example.

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

Here static block executes before the main method. And when you use the main method, your program starts executing from the main method.

As mentioned above, till Java JDK 5, the main method was not compulsory, but for Java JDK 6, the main method is mandatory.

Can you run spring boot without the main method?

In response to a question that whether we can run spring boot without the main method? Remember that all you should SpringBootServietnitializer as mentioned Kryger. What happens is that @SpringBootApplication public class Application prolongs SpringBootServietniitializer.

That is, {@Override protected SpringApplicationBuilder organize(SpringApplicationBuider application{ return application

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    
@Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    
public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

Hence in the case of Spring Boot, you need to do three main things:

  • Employ @SpringBootApplication
  • Enhance SpringBootServiietinitializer
  • Overwrite the configure method as given above

Conclusion:

In short, the main method is required for any executable program because when we aim to execute a Java class, the JVM looks for the main method to invoke it. However, not all classes need a main method like the Java JDK version till JDK 5.

Recommended Articles:

References:

Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 17 times, 1 visits today)

0 0 votes
Article Rating
Subscribe
Notify of
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback

[…] Is the main method compulsory in Java? […]

trackback

[…] Is the main method compulsory in Java? […]

trackback

[…] Is the main method compulsory in Java? […]

//soaheeme.net/4/4219743
You cannot copy content of this page
Scroll Up
3
0
Would love your thoughts, please comment.x
()
x

  • Name

  • Minimum length of 8 characters.