Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why main method is static in java [duplicate]

Tags:

java

main

static

I have heard some people saying " if main is not static then JVM could create an object of class containing main and call that main through object.
But the problem is how JVM knows which constructor to call in case of overloaded constructors or even if there is only one paramaterized constructor, then what to pass."

Is that the correct reason?
Because how can object of class be created without entering into main function?
Please give your views on this. If that is not the right reason, then what's the correct reason?

like image 309
Happy Mittal Avatar asked Jul 05 '10 17:07

Happy Mittal


People also ask

Why main method in Java is always static?

When java runtime starts, there is no object of the class present. That's why the main method has to be static so that JVM can load the class into memory and call the main method. If the main method won't be static, JVM would not be able to call it because there is no object of the class is present.

Why main method is a static method?

The main() method is static so that JVM can invoke it without instantiating the class. This also saves the unnecessary wastage of memory which would have been used by the object declared only for calling the main() method by the JVM.

What happens if the main () isn't declared as static?

Bootstrap class loader searches for main function in the class file, if main function is not declared as static, it will trough an error because declaring function as static allows it to be called without instantiating that class file where the main function is.

Why main method is declared as public and static?

We know that anyone can access/invoke a method having public access specifier. The main method is public in Java because it has to be invoked by the JVM. So, if main() is not public in Java, the JVM won't call it. That's all about why the main method is declared public and static in Java.


2 Answers

It's just a convention. The Java language designers could have easily decided that you must designate a class to be instantiated, making its constructor the main method. But calling a static method is just as effective and doesn't require a class to be instantiated first.

Also, if the class has a superclass, you could alter the behavior of program startup by changing a superclass (since superclass constructors must be called before subclasses), perhaps unintentionally. Static methods don't have this problem.

The main method is static because it keeps things simpler, but if they wanted to make it more complicated, they could have.

like image 121
benzado Avatar answered Sep 23 '22 01:09

benzado


"Is this reason correct?"

It is at some degree, although it was never specifically explained like that.

We could have the convention to also invoke the constructor with String...args , that would imply you need at least an object to run, but ( probably ) the designers thought that wasn't needed.

For instance, there is no technical impediment to have something like this:

 class Hello {
       public void main(String...args){
           System.out.println("Hello, world");

       }
 }

As a metter of fact, Java creates a no-arg constructor for you if you don't specify one, it would be very easy to include a var args constructor too, if the class contained a main method, and create under the hood the following code:

 class Hello {
      // created by the compiler
      public Hello(){}
      public Hello( String ... args ) {
          this();
          main( args );
      }
      // end of code created by the compiler
      public void main( String ... args ) {
           System.out.println("Hello, world!");
      }
  }

But that would create unneeded code, and extra allocated object that in this case wouldn't do anything; two constructors ( instead of one ) etc. At the end it would look like just too much magic.

Is up to the language designers. In this case they probably thought it would be simpler not to do it and just validate if the invoked class had the special signature of a method called public static void main( String [] args )

BTW you can have a Hello! world program in Java without main method, but it throws java.lang.NoSuchMethodError: main

public class WithoutMain {
    static {
        System.out.println("Look ma, no main!!");
        System.exit(0);
    }
}

$ java WithoutMain
Look ma, no main!!

I know it is not an alternative, but yet, it is interesting isn't?

like image 30
OscarRyz Avatar answered Sep 19 '22 01:09

OscarRyz