[Java] What is Public static void main (String [] args)?

Those who are doing java will definitely see ** public static void main (String [] args) **.

It is a necessary method to execute.

However, if you make a mistake in any one of these, an error will occur. Why?

I'm curious, so I'd like to introduce it to the same beginners in the future.

■ Let's understand the meaning first

Hello.java


class Hello{
    public static void main(String[] args) {
        System.out.println("Hello World");        
    }
}

And the content is like this.

Surprisingly ** static is also important for understanding instances ** so let's remember (commandment)

item meaning
public Can be referenced from anywhere(Access modifier)
static Instance possible(new)Can be used from the outside without
void No return value
main Method name
String[] Receive the argument as a String type array
args Abbreviation for arguments in the plural form of argument name and argument (Japanese translation: argument)

Who are args?

String [] args are values (command line arguments) specified when the program starts.

The argument name args is customarily used as args, but you can use another name. The reason why args is used by convention is that Java is the successor to C and inherits the convention of C.

What is static?

The main method requires a static qualifier. But why don't you think?

It usually requires ** instantiating a class with new if you want to use the methods of the class **. (This is a problem that too many people do not remember beginners)

SubClass sub = new SubClass();
sub.main(new String[] {"Hello SubClass!"});

However, the static modifier allows you to access the method without instantiating it with new.

SubClass.main(new String[] {"Hello SubClass!"});

This means that executing the java command does not instantiate the main class, so the main method being executed must have the static modifier.

I need this to run it for the time being

  1. The method name is main
  2. Array with String type argument (variadic argument)
  3. Static modifier is given

If you try to execute a java command for a class that does not meet this condition, it will fail as follows.


error:The main method cannot be found in class HelloWorld. Define the main method as follows:
   public static void main(String[] args)

Also, if the return value is set to something other than void, the following error will occur.

error:The main method should return a value of type void in class HelloWorld.
Define the main method as follows:
   public static void main(String[] args)

Recommended Posts

[Java] What is Public static void main (String [] args)?
[Java] Meaning of Public static void main (String [] args)
[Java] What is Public static void main (String [] args)?
What is java
What is Java <>?
What is the main method in Java?
What is Java
What is Java Encapsulation?
What is Java API-java
[Java] What is flatMap?
[Java] What is JavaBeans?
[Java] What is ArrayList?
What is Java Assertion? Summary.
What is a Java collection?
[Java] What is jaee j2ee?
[Java] What is class inheritance?
[Java basics] What is Class?
What is java escape analysis?
What is JVM (Java Virtual Machine)?
What is thread safe (with Java)
[Java] What is Concurrent Modification Exception?
What is a lambda expression (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 Java and Development Environment (MAC)
What is a class in Java language (2 /?)
Java static
What is the volatile modifier for Java variables?
Check static and public behavior in Java methods