Detailed sorting of interview questions in Java main method

Detailed sorting of interview questions in Java main method

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 can 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.

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

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

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"?

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.

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.

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.

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.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>