Ali II: can the main method be inherited?
Yesterday, a netizen in the wechat group posted his interview with Ali in the group. One of the interviews, he PUA other netizens in the group. The interview question is: can the main method in Java be inherited?
When we first started learning Java programs, the first piece of code to run must be the main method. The format of the main method is as follows:
public static void main(String[] args) { }
So what's special about the main method? Today, let's take a brief look.
First, define the format of the main method:
Public: the main method is loaded by the JVM at startup. Public has the highest access permission, so it needs to be declared public;
"Static": the method is called either through the object or through the class. Since the main method is called by the virtual machine, there is no need to generate an object, so it can be declared as static;
"Main": as for why the method name is called main, I think it should refer to the method name of C language;
Void: when the main method exits, no relevant return value needs to be returned, so it is void;
"String []": this string array is used to accept user input parameters at runtime; Because strings are universal in Java, using strings is the best choice; In the case of array, because we have more than one parameter, the array must be appropriate;
But since jdk1 5 after introducing dynamic parameters, String [] array can also use String Args.
public static void main(String... args){ }
Except that the main method specified by the JVM above is special, other main methods are no different from ordinary static methods.
Can the main method be overloaded?
This is OK. For example, we overload a method:
public class Main { public static void main(String args) { System.out.println("hello world:" + args); } public static void main(String[] args) { main("test"); } }
Obviously, there is no problem in compiling and running. Except for the main method specified by the JVM as the application entry, other main methods are relatively common methods.
Can the main method be called by other methods?
public class Main { private static int times = 3; public static void main2(String[] args) { times--; main(args); } public static void main(String[] args) { System.out.println("main Method execution:" + times); if (times <= 0) { System.exit(0); } main2(args); } }
Run the following code and you can find that the code can execute normally:
main Method execution:3 main Method execution:2 main Method execution:1 main Method execution:0
Therefore, even the main method as an application entry can be called by other methods, but pay attention to the closing method of the program and don't fall into an endless loop.
Can the main method inherit?
We have learned before that when the class inherits, the subclass can inherit the methods and variables of the parent class. Then, when the parent class defines the main method and the subclass does not have the main method, can it inherit the main method of the parent class to run the program normally?
public class Main { public static void main(String[] args) { System.out.println("hello world"); } }
Define subclasses:
public class Main2 extends Main { }
At this time, when we run subclass Main2, we can find that hello world is also printed, which shows that the main method can also be inherited. There is also a hidden situation. It is also possible for subclasses to define their own main methods and hide the implementation in the parent class.
public class Main2 extends Main { public static void main(String [] args) { System.out.println("hello world Main2"); } }
At this time, the content of the subclass itself will be printed: hello world Main2.
In this way, except that the main method is special as the entry of the application, it is no different from the normal static method in other cases.