This is an article for organizing information by java beginners. We would appreciate it if you could point out any corrections or additional information that should be included.
public static void main(String[]args){
//statement
}
・ Methods defined in this form in many cases (exception patterns are described later in the article) In the statement,
-Method (entry point) that is the ** starting point ** of the Java program This is the first point to be referenced when starting a Java program. Since the main method is the starting point, methods defined in other classes will not be executed unless they are called in the main method.
Also, if the main method does not exist, you will get an error like this:
Exception in thread "main" java.lang.NoSuchMethodError: main
The public qualifier is a qualifier that allows access from all classes. The main method is the starting point of the Java program, and it is necessary to add the public qualifier assuming access from various classes.
static The generic name of fields and methods with the static keyword is called ** static member **. Among them, the method with the static keyword is called ** static method (class method) **. In other words, the main method is a static method.
Static methods have the following three features.
Here, the three features are important. Since the main method is the starting point of the Java program, it is possible that no instance has been created in the Java virtual space at the time of startup. To handle this situation, the main method needs to have a static keyword.
Although it is a supplementary content, it is not necessary to add the static keyword because the method described in another class and described once the object is created does not need to be called when the program is started. It will be. void Indicates that this method does not return a return value.
main Method name. The identifier referenced by the JVM. String[]args The rule here is that you need to specify a String type array or a String type variadic argument. (I couldn't find the reason, so I'll put it on hold) Therefore, the part of args that corresponds to the argument can be changed. However, it seems that there are many cases where args are generally used.
From the description so far, it turns out that the main method is a special method. Here is one pattern that you should pay attention to when actually using it.
public class Cleric {
String name = "Yudai";
int hp = 50;
final int maxHp = 50;
int mp = 30;
final int maxMp = 30;
public static void main(String[] args) {
//TODO auto-generated method stub
Cleric cleric = new Cleric();
cleric.selfAid();
}
public void selfAid() {
System.out.println(this.name + "Chanted "self-healing"!");
this.mp -= 5;
this.hp = this.maxHp;
System.out.println("HP has recovered to the maximum!");
}
In this way, it is a pattern that the main method and another method (here, the selfAid method coexist) in one class. Here, the main method is described in the Cleric class, but since the main method is actually the method that is the starting point of every class, it has nothing to do with the Cleric class. However, in this form, the main method looks like a method related to the Cleric class, which can cause confusion when the amount of code increases. .. So, as a workaround, I think it is safe to generate an independent class (for example, Main class) and prepare it as a class that executes only the main method.
Also, as a supplement, it is good to remember that you basically write code in one class for one file.
public static void main(String args[]) { }
public static void main(String...args) { }
public strictfp static void main(String[]args) { }
public static void main(final String[]args) { }
final static synchronized strictfp void main(final String[]args) { }
Thank you very much. that's all.
[Reference] ・ "Introduction to Java that can be understood clearly, 3rd edition" [Reference URL] -[Introduction to Java] Explanation of main method, its arguments (args), and return value https://www.sejuku.net/blog/65082 -Explanation of Java's main () method https://www.codeflow.site/ja/article/java-main-method ・ Relationship between main method and class https://www.javadrive.jp/start/about/index4.html -Method of the first class to be called https://www.javadrive.jp/start/const/index3.html ・ [I want to hold it firmly] What is Java's main method? And how to use https://eng-entrance.com/java-basic-main
Recommended Posts