10 classic javamain method interview questions

The following is what I think is a classic interview question about Java main method. It is not so much a Java interview question, but also some of the most basic knowledge questions of Java. Share it with you. If there are any errors, please point out.

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

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

Before Java 7, you could run Java classes by using static initialization. However, it has not worked since Java 7.

  2. The parameter required by the main () method is not a string array?

No, the argument to the main () method must be a string array.

However, when introducing variable parameters, you can pass string type variable parameters as parameters to the main () method. The argument must 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 not acceptable.

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 must instantiate its class when calling the main method.

When instantiating, you have to call the constructor of the class. If the constructor of this class has parameters, ambiguity will occur at that time.

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 as 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 declaration of "static" from the main () method, although the compilation can still succeed, it will cause the program to 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 overload the main () method?

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

In order 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 statement, the compilation can be successful. However, you can't run Java programs. 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 do we not need access modifiers?

No, the main () method must be public. You can't define the main () method as private and protected, and you can't use the access modifier.

This is to allow the JVM to access the main () method. If you do not 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 a static method, and in Java, static methods will be combined at compile time, so you can't override static methods in Java.

  9. Can we terminate the main method in Java?

You can terminate the main method in Java. The JVM has no problem with this.

  10. Can we synchronize the main method in Java?

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

Original English: Java interview questions on main() method

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>