Why Is the Java Main Method Static

Why is the Java main method static?

The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:

public class JavaClass{
protected JavaClass(int x){}
public void main(String[] args){
}
}

Should the JVM call new JavaClass(int)? What should it pass for x?

If not, should the JVM instantiate JavaClass without running any constructor method? I think it shouldn't, because that will special-case your entire class - sometimes you have an instance that hasn't been initialized, and you have to check for it in every method that could be called.

There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That's why main is static.

I have no idea why main is always marked public though.

Why the main method should be in static

Since main method is static Java virtual Machine can call it without creating any instance of class which contains main method.

If main method were not declared static than JVM has to create instance of main Class and since constructor can be overloaded and can have arguments there would not be any certain and consistent way for JVM to find main method in Java.

also refer this for detailed and very well explained answers.

Does the main method in Java have to be static?

Not all your methods must be static, only the main entry point for your application. All the other methods can remain non-static but you will need to use a reference of the class to use them.

Here's how your code would look like:

public class Sheet {
public static void main(String[] args) {
Sheet sheet = new Sheet();
sheet.myMethod();
}

public void myMethod(){
System.out.println("hi there");
}
}

The explanation for your concerns are explained here (there's no need to dup all the info here):

  • Why is the Java main method static?
  • What does the 'static' keyword do in a class?
  • In laymans terms, what does 'static' mean in Java?

Why main() is declared public and static in java

public - The main method is called by the JVM to run the method which is outside the scope of the project therefore the access specifier has to be public to permit a call from anywhere outside the application.

static - When the JVM makes a call to the main method there is no object that exists for the class being called therefore it has to have static method to allow invocation from class.

void - Java is a platform independent language, therefore if it returns some value, then the value may have a different meaning between different platforms so unlike C it can not assume a behavior of returning value to the operating system.

Why main method is static in java

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.

Why am I being forced to change methods and varibles to static when calling them from my main method?

It can be a bit confusing to get out of "static land" once you are in your main() method. One easy way is to have another object contain your "real" (non-static) top level code and then your main method creates that object and starts it off.

public static void main() {
MyEngine engine = new MyEngine();
// core logic inside of start()
engine.start();
}

I hope that this was clear enough for you. Good luck Maisy!

Why is the Java main method static?

The method is static because otherwise there would be ambiguity: which constructor should be called? Especially if your class looks like this:

public class JavaClass{
protected JavaClass(int x){}
public void main(String[] args){
}
}

Should the JVM call new JavaClass(int)? What should it pass for x?

If not, should the JVM instantiate JavaClass without running any constructor method? I think it shouldn't, because that will special-case your entire class - sometimes you have an instance that hasn't been initialized, and you have to check for it in every method that could be called.

There are just too many edge cases and ambiguities for it to make sense for the JVM to have to instantiate a class before the entry point is called. That's why main is static.

I have no idea why main is always marked public though.

Why main method needs to be static?

The only answer that can be reliably given is "because the JLS says so". Quote:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String.



Related Topics



Leave a reply



Submit