10 classic interview questions for the Java main method

  • 2020-04-01 04:38:13
  • OfStack

Share with everyone, if there is a mistake, please point out.

1. How to define a class without using the main method?

No, we can't run a Java class without the main method.

Before Java 7, you could run Java classes using static initialization. However, starting with Java 7, this didn't work.

2. The main() method does not require an array of strings?

No, the argument to the main() method must be an array of strings.

However, when introducing arguments, you can pass arguments of string type to the main() method as arguments. The argument has to be an array.


package com.instanceofjava;
public class MainMethod
{
public static void main(String args[])
{
}
}

3. Can we change the return type of the main() method?

No, the return type of the main() method can only be null. Any other type is unacceptable.


package com.instanceofjava;
public class A
{
public static int main(String[] args)
{
 return 1;  //run time error : No main method found
}
}

4. Why must the main() method be static?

The main() method must be static.

If main() is allowed to be non-static, the JVM has to instantiate its class when the main method is called.

At instantiation, the constructor of the class is also called. If the constructor of this class has arguments, then there will be ambiguity.

For example, in the following program, what parameters does the JVM pass when instantiating class "A"?


package com.instanceofjava;
public class A
{
public MainMethod(int i)
{
//Constructor taking one argument
}
 public void main(String[] args)
{
//main method as non-static
}

5. Can we declare the main() method to be non-static?

No, the main() method must be declared static so that the JVM can call the main() method without instantiating its class.

If you remove the "static" declaration from the main() method, the compile will still succeed, but the program will fail at run time.


package com.instanceofjava;
public class A
{
public void main(String[] args)
{
System.out.println("indhu");     //Run time error
}
}

6. Can we override the main() method?

Yes, we can override the main() method. A Java class can have any number of main() methods.

To run a Java class, the main() method of the class should have a declaration such as "public static void main(String[] args)". If you make any changes to this declaration, the compilation will be successful. However, you cannot run a Java program. You will get a runtime error because the main method cannot be found.


package com.instanceofjava;
public class A
{
public static void main(String[] args)
{
System.out.println("Indhu");
 }
void main(int args)
{
System.out.println("Sindhu");
}
long main(int i, long d)
{
System.out.println("Saidesh");
return d;
}
}

7. Can we declare the main() method private or protected, or not access modifiers?

No, the main() method must be public. You can't define the main() method as private and protected, and you can't do it without accessing modifiers.

This is to give the JVM access to the main() method. If you don't define the main() method as public, the compilation will succeed, but you will get a runtime error because the main method cannot be found.


package com.instanceofjava;
public class A
{
private static void main(String[] args)
{
//Run time error
}
}

8. Can we override the main method in Java?

No, you can't override the main method in Java. This is because the main method is static, and in Java static methods are combined at compile time, so you can't override static methods in Java.

Can we terminate the main method in Java?

You can terminate the main method in Java. The JVM is fine with that.

Can we synchronize the main method in Java?

Yes, the main method can be synchronized in Java, and the synchronized modifier is allowed in the declaration of the main method so that the main method can be synchronized in Java.

The above is the entire content of this article, I hope to help you solve the Java main method interview questions.


Related articles: