The main Method


The main() method is required and you will see it in every Java program:


public static void main(String[] args)

Any code inside the main() method will be executed. You don't have to understand the keywords before and after main. You will get to know them bit by bit while reading this tutorial.

For now, just remember that every Java program has a class name which must match the filename, and that every program must contain the main() method.


System.out.println()

Inside the main() method, we can use the println() method to print a line of text to the screen:


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


Note: In Java, each code statement must end with a semicolon.


Test Yourself With Exercises

Exercise:

Insert the missing part of the code below to output "Hello World".

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


Jay Kakadiya

Jay Kakadiya Creator

I am a computer field, & i am Web developer.

Suggested Creators

Jay Kakadiya