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

 In Java, the main() method is the entry point of a program, and is called by the Java runtime system when the program is executed. The main() method is required to be declared as public static void, which means that it is a public static method that does not return a value.

If the main() method is not declared as static, the Java compiler will generate an error, because the main() method must be static in order to be called by the Java runtime system.

For example, the following code will generate a compile-error:

public class MyClass { public void main(String[] args) { // code goes here } }

To fix the error, the main() method must be declared as static:
public class MyClass { public static void main(String[] args) { // code goes here } }

t's important to note that the main() method is just a regular method, and can be called from other methods in the same class. However, it is typically only called by the Java runtime system when the program is executed.

Comments

Popular posts from this blog

Why does the java array index start with 0?