Explanation Public Static
Explanation Public Static
static : it is access modifier that means when the java program is load then it will create
the space in memory automatically.
void : it is a return type i.e it does not return any value.
main() : it is a method or a function name.
string args[] : its a command line argument it is a collection of variables in the string
format
class GeeksforGeeks {
public static void main(String[] args)
{
System.out.println("I am a Geek");
}
}
I am a Geek
Explanation:
Every word in the public static void main statement has got a meaning to the JVM.
1. Public: It is an Access modifier, which specifies from where and who
can access the method. Making the main() method public makes it globally
available. It is made public so that JVM can invoke it from outside the class as it
is not present in the current class.
class GeeksforGeeks {
private static void main(String[] args)
{
System.out.println("I am a Geek");
}
}
Error: Main method not found in class, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application
2. Static: It is a keyword which is when associated with a method, makes
it a class related method. The main() method is static so that JVM can invoke it
without instantiating the class. This also saves the unnecessary wastage of
memory which would have been used by the object declared only for calling
the main() method by the JVM.
filter_none
edit
play_arrow
brightness_4
class GeeksforGeeks {
public void main(String[] args)
{
System.out.println("I am a Geek");
}
}
Error: Main method is not static in class test, please define the
main method as:
public static void main(String[] args)
3. Void: It is a keyword and used to specify that a method doesn’t return
anything. As main() method doesn’t return anything, its return type is void. As
soon as the main() method terminates, the java program terminates too. Hence,
it doesn’t make any sense to return from main() method as JVM can’t do
anything with the return value of it.
filter_none
edit
play_arrow
brightness_4
class GeeksforGeeks {
public static int main(String[] args)
{
System.out.println("I am a Geek");
return 1;
}
}
Error: Main method not found in class, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application
4. main: It is the name of Java main method. It is the identifier that the
JVM looks for as the starting point of the java program. It’s not a keyword.
filter_none
edit
play_arrow
brightness_4
class GeeksforGeeks {
public static void myMain(String[] args)
{
System.out.println("I am a Geek");
}
}
Error: Main method not found in class, please define the main
method as:
public static void main(String[] args)
or a JavaFX application class must extend
javafx.application.Application
5. String[] args: It stores Java command line arguments and is an array
of type java.lang.String class. Here, the name of the String array is args but it is
not fixed and user can use any name in place of it.
class GeeksforGeeks {
// javac GeeksforGeeks.java
// java GeeksforGeeks 1 2 3
public static void main(String[] args)
{
for (String elem : args)
System.out.println(elem);
}
}
1
2
3
Apart from the above mentioned signature of main, you could use public static void
main(String args[]) or public static void main(String… args) to call the main
function in java. The main method is called if it’s formal parameter matches that of an
array of Strings.
Got what you were looking for? Learn more and become self sufficient. Start learning
Data Sructures & Algorithms with the help of the most trusted DSA Self
Paced course, and that too at the most student-friendly price.