0% found this document useful (0 votes)
17 views

Gp-Java-Main Method in Java

The main() method in Java must be declared as public static void main(String args[]) to ensure it can be called properly by the Java Virtual Machine (JVM). It is the entry point of any Java application where program execution begins. The main() method prints "Hello World" without needing to create an instance of the class containing it due to being static. It does not return a value, specified by its void return type.

Uploaded by

Sai Ram Nani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Gp-Java-Main Method in Java

The main() method in Java must be declared as public static void main(String args[]) to ensure it can be called properly by the Java Virtual Machine (JVM). It is the entry point of any Java application where program execution begins. The main() method prints "Hello World" without needing to create an instance of the class containing it due to being static. It does not return a value, specified by its void return type.

Uploaded by

Sai Ram Nani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

main() Method

Before explaining the java main() method, let’s first create a simple program to
print Hello World. After that, we will explain why the main() method in java is
public static void main(String args[]).

public class HelloWorld {


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

● public: the public is an access modifier that can be used to specify who
can access this main() method. It simply defines the visibility of the
method. The JVM calls the main() method outside the class. Therefore
it is necessary to make the java main() method as public.

● static: static is a keyword in java. We can make static variables and


static methods in java with the help of the static keyword. The main
advantage of a static method is that we can call this method without
creating an instance of the class. JVM calls the main() method without
creating an instance of the class, therefore it is necessary to make the
java main() method static.

● void: void is a return type of method. The java main() method doesn’t
return any value. Therefore, it is necessary to have a void return type.

● main: main is the name of the method. It is a method where program


execution starts.

● String args[]: String in java is a class that is used to work on Strings


and args is a reference variable that refers to an array of type String. If
you want to pass the argument through the command line then it is
necessary to make the argument of the main() method as String args[].

You might also like