DEV Community

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

Posted on

4 1

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.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (7)

Collapse
 
gklijs profile image
Gerard Klijs
Comment hidden by post author
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
gklijs profile image
Gerard Klijs
Comment hidden by post author
 
Sloan, the sloth mascot
Comment deleted
 
alainvanhout profile image
Alain Van Hout
Comment hidden by post author
 
Sloan, the sloth mascot
Comment deleted
 
alainvanhout profile image
Alain Van Hout
Comment hidden by post author

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

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay