• Contact Me
  • About Me
  • Privacy Policy
  • Home

Why main method is static in Java

  • Home
  • Interview Questions
  • Why main method is static in Java
How To Send HTTP Request GET/POST In Java
June 18, 2013
SOAP Vs RESTFul for Mobile Services – Which one to use ?
November 7, 2013
Published by Mudassir Shahzad on June 21, 2013
Categories
  • Interview Questions
  • Java Core
Tags
  • java
  • java main
  • java se
  • java variables
  • JNI
  • JVM

This is one of the basic question asked in interviews or in gatherings of professionals.

So here we go,

Main method is static in java because

 

This is just a convention. In fact, even the name main(), and the arguments passed in are purely convention.

  1. Since C and C++ also has similar main method which serves as entry point for program execution, and java was inspired from C, following that convention will only help Java.
  2. Since main method is static, the JVM Java virtual Machine can call it without creating any instance of class which contains main method.

When you run java.exe (or javaw.exe on Windows), what really happens is a couple of Java Native Interface (JNI) calls. These calls load the DLL that is really the JVM (that’s right – java.exe is NOT the JVM). JNI is the tool that we use when we have to bridge between the virtual machine world, and the world of C, C++, etc… (Read More About JNI)

The reverse is also true – It is not possible (at least to my knowledge) to actually get a JVM running without using JNI.

Basically, java.exe is a super simple C application that parses the command line, creates a new String array in the JVM to hold those arguments, parses out the class name that you specified as containing main(), uses JNI calls to find the main() method itself, then invokes the main() method, passing in the newly created string array as a parameter. This is very, very much like what you do when you use the reflection API in Java – It just uses confusingly named native function calls instead.

Another reason is that if main was not static it would need an instance of the object to be executed. But it must be called from scratch, without constructing the object first, since it is usually the task of the main() function (bootstrap), to parse the arguments and construct the object, usually by using these arguments/program parameters.

It would be perfectly legal for you to write your own version of java.exe (the source is distributed with the JDK), and have it do something entirely different. In fact, that’s exactly what we do with all of our Java based apps.

 

Don’t believe it ? Here is how we do it

 

Each of our Java apps has its own launcher. We primarily do this so we get our own icon and process name, but it has come in handy in other situations where we want to do something besides the regular main() call to get things going (For example, in one case we are doing COM interoperability, and we actually pass a COM handle into main() instead of a string array).

So, long and short: the reason main() is static is because that’s convenient. The reason it’s called ‘main’ is because it had to be something, and main() is what they did in the old days of C (and in those days, the name of the function was important). I suppose that java.exe could have allowed you to just specify a fully qualified main method name, instead of just the class (java com.myompany.Foo.someSpecialMain) – but that just makes it harder on IDEs to auto-detect the ‘launchable’ classes in a project.

Because “main” is what an IDE looks for 😉

 

References

Oracle Java Virtual Machine Specification

Related posts:

Why Hibernate should be chosen for projects ? Advantages of Hibernate
What are Jax-RS, Jax-WS and Jax-B ?
Java Versions, Features and History

Share
0
Mudassir Shahzad
Mudassir Shahzad

Related posts

November 8, 2018

New JDK 11 features that I personally like and use


Read more
January 16, 2015

The famous java == and .equals()


Read more
December 30, 2014

What Is Unicode? UTF-8, UTF-16 and UTF-32 etc ?


Read more

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Find Us On

Follow Me!

  • Find Me

    • About Me
    • Contact Me
    © 2023 Mudassir Shahzad. All Rights Reserved. Muffin group