What is the main method in Java?

Introduction

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.

What is the main method

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,

  1. Variable execution statement
  2. Calculation formula statement
  3. Statement of instruction execution Write various sentences such as. Each sentence is processed in order from the top.

-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

Decompose the main method

public modifier

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.

  1. The method itself belongs to the class
  2. The method alter ego is set in the instance
  3. Can be called without creating an instance

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.

Precautions when using the main method

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.

How to write various main methods


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

What is the main method in Java?
What is the pluck method?
What is the initialize method?
Call the super method in Java
What is java
What is Java
What is a class in Java language (3 /?)
What is the best file reading (Java)
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
What is Java Encapsulation?
What is Java technology?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Pullback doing in The Composable Architecture
[Java] Handling of JavaBeans in the method chain
What is the Java Servlet / JSP MVC model?
The order of Java method modifiers is fixed
The intersection type introduced in Java 10 is amazing (?)
What is the volatile modifier for Java variables?
[Java] Read the file in src / main / resources
[Java] Something is displayed as "-0.0" in the output
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
What is Java Assertion? Summary.
Which is better, Kotlin or Java in the future?
What is the BufferedReader class?
[JAVA] What is the difference between interface and abstract? ?? ??
[Java] Is it unnecessary to check "identity" in the implementation of the equals () method?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java] What is Public static void main (String [] args)?
What is the constructor for?
What is the difference between Java EE and Jakarta EE?
[Java basics] What is Class?
What is java escape analysis?
Java is the 5th day
Why the get method is needed in the Calendar class
What is ... (3 dots) found in the Java source? Variadic arguments to know from
The story that .java is also built in Unity 2018
How to get the class name / method name running in Java
I translated [Clone method for Java arrays] as the Clone method in Java arrays.
What is the LocalDateTime class? [Java beginner] -Date and time class-
[Android, Java] Convenient method to calculate the difference in days
What is the representation of domain knowledge in the [DDD] model?
Create a method to return the tax rate in Java
What is the difference between an action and an instance method?
Guess the character code in Java
'% 02d' What is the percentage of% 2?
Automatic photo resizing method in Java
Specify the java location in eclipse.ini
Java comparison using the compareTo () method
Unzip the zip file in Java
[Java] What got caught in encapsulation
What is thread safe (with Java)
What is a snippet in programming?
What is @Autowired in Spring boot?
[Java] What is Concurrent Modification Exception?
Parsing the COTOHA API in Java
What is a lambda expression (Java)