can we declare main method as non static in java?

No, JVM invokes main method even before the instantiation of the class. As non-static members or methods cannot be called with the class name directly so main() method should be declared as static. Example

public class Main
{
	public void main(String[] args) {
		System.out.println("Non static main method.");
	}
}

Output

Error: Main method is not static in class Main, please define the main method as:
   public static void main(String[] args)

Java interview questions on main method

Please follow and like us:
Content Protection by DMCA.com