DEV Community

Cover image for Java Main Method: Why so Many Words?
Julia
Julia

Posted on

Java Main Method: Why so Many Words?

public static void main(String[] args){
// code
}

ugh

"Seriously? I have to type this every time i write a program? That is a lot and I miss JavaScript! 😭" These were always my thoughts when I was in college just starting with Java after learning JavaScript for a bit.

What we see above is the main method in Java. Let's break it down and talk about each word in this line of code.

Public
Public in Java is an access modifier. Access modifiers specify the accessibility or scope of a method, constructor, or a class. There are four access modifiers:

  1. Private: the access level of this modifier is only within the class and cannot be accessed from outside of the class.
  2. Public: the access level of this modifier is everywhere and can be accessed within the class, outside the class, within the package and outside the package.
  3. Protected: the access level of this modifier is within the package and outside the package through child classes, without which it cannot be accessed from outside.
  4. Default: the access level of this modifier is only within the package, and cannot be accessed from outside the package.

The main method has to be public because we want access to it.

Static
In Java, a static member of a class is a member that isn't associated with an instance of a class. This member belongs to the class itself and can be accessed without first creating a class instance. A method that is declared with the static keyword is associated with the class itself, and therefore we don't have to create an object from a class before using static methods defined by the class.

If the main method is not static, JVM wouldn't be able to call it because there is no class object present. That's why the main method must be static so that JVM can call the main method which is not associated with an instance of a class.

Void
This one is simple. The keyword void is used at method declaration to indicate that the method should not have a return value, and this is why main method is void

Main
Main is just a name for the method, and when we run a Java program, it looks for the name.

Below is a simple program that calculates the Fibonacci of n recursively and prints out the result.

Top comments (7)

Collapse
 
gklijs profile image
Info Comment hidden by post author - thread only accessible via permalink
Gerard Klijs

Not really, as most Java will use Spring, which means you can use start.spring.io/. And otherwise the IDE can help you.

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
gklijs profile image
Info Comment hidden by post author - thread only accessible via permalink
Gerard Klijs

Sorry didn't meant to offend you. It's a similar way like how typescript can help with javascript being a dynamically typed language. Also nobody is forcing you to use Java.

 
Sloan, the sloth mascot
Comment deleted
 
alainvanhout profile image
Info Comment hidden by post author - thread only accessible via permalink
Alain Van Hout

I think you are reading a lot into his original comment. An IDE can help you, as it daily helps me and many others as well. That's not a belittling statement.

 
Sloan, the sloth mascot
Comment deleted
 
alainvanhout profile image
Info Comment hidden by post author - thread only accessible via permalink
Alain Van Hout

His first comment didn't include that, yet you responded unambiguously hostile. Also, 'your kind'? Now that's belittling.

But yes, you are fully entitled to write whatever you want. More power to you.

Some comments have been hidden by the post's author - find out more