Click for PDF manual V2.0 – > interview | springboot | springcloud | programming ideas

Ali: Can the main method be inherited?

Yesterday, a netizen in wechat group posted the process of interviewing Ali. One of them interviewed him in the group PUA other net friends. The interview question is: Can the Main method in Java be inherited?

When we first start learning Java programs, the first piece of code to run will be the main method, which has the following format:

public static void main(String[] args) {

}
Copy the code

So what’s special about the main method? Today we’re going to look at it briefly.

First, we 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 by an object or by a class. The main method is called by a virtual machine, so it does not need to generate an object.

“Main” : As to why the method name is called main, I think it is a reference to the C language method name.

“Void” : when the main method exits, there is no return value to return, so void;

“String[]” : This array of strings is used to accept user-input arguments at runtime; Because strings are universal in Java, using strings is optimal; Arrays, because we have more than one parameter, so arrays must be ok;

However, since the introduction of dynamic parameters in JDK1.5, String[] arrays can also use strings… Args.

public static void main(String... args){

}
Copy the code

With the exception of the special main method specified by the JVM above, the main methods are no different from normal static methods.

Can the main method be overloaded?

This is ok, let’s say we overload it with 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"); }}Copy the code

Compile and run, and there’s obviously nothing wrong with the main method, which the JVM specifies as an entry point to the application.

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 :" + times);} public static void main(String[] args) {system.out.println ("main :" + times); if (times <= 0) { System.exit(0); } main2(args); }}Copy the code

Run the code and you can see that it works:

3 Run the main method :2 Run the main method :1 Run the main method :0Copy the code

So even the main method, which is an entry point to your application, can be called by other methods, but be careful how you close your application so you don’t get stuck in an endless loop.

Can the main method inherit?

If the parent class defines a main method, but the subclass does not have a main method, can inherit the main method of the parent class and run the program normally?

public class Main { public static void main(String[] args) { System.out.println("hello world"); }}Copy the code

Define subclasses:

public class Main2 extends Main {
}
Copy the code

If we subclass Main2, we’ll see that we also print Hello World, which means that main is also inheritable. There is also an obvious case of hiding, if a subclass defines its own main method and hides the implementation from the parent class, then that is also ok.

public class Main2 extends Main { public static void main(String [] args) { System.out.println("hello world Main2"); }}Copy the code

This will print the subclass’s own content: Hello World Main2.

In this case, the main method is no different from normal static methods, except that it is a special entry point to the application.

Recommend 3 original Springboot +Vue projects, with complete video explanation and documentation and source code:

Build a complete project from Springboot+ ElasticSearch + Canal

  • Video tutorial: www.bilibili.com/video/BV1Jq…
  • A complete development documents: www.zhuawaba.com/post/124
  • Online demos: www.zhuawaba.com/dailyhub

【VueAdmin】 hand to hand teach you to develop SpringBoot+Jwt+Vue back-end separation management system

  • Full 800 – minute video tutorial: www.bilibili.com/video/BV1af…
  • Complete development document front end: www.zhuawaba.com/post/18
  • Full development documentation backend: www.zhuawaba.com/post/19
  • Online demos: www.markerhub.com/vueadmin

【VueBlog】 Based on SpringBoot+Vue development of the front and back end separation blog project complete teaching

  • Full 200 – minute video tutorial: www.bilibili.com/video/BV1PQ…
  • Full development documentation: www.zhuawaba.com/post/17
  • Online demos: www.markerhub.com:8084/blogs

If you have any questions, please come to my official account [Java Q&A Society] and ask me