Java Program Structure

Before proceed to learn Java, it is important to understand the basic structure of a Java program.

To know about the structure of a Java Program, just look at the following simple example:

/* Java Program Example - Java Program Structure
 * This is a simple Java program, prints
 * "Hello World" on the Screen

public class JavaProgram

    /* This
     * is
     * a
     * multi-line
     * comments
    public static void main(String args[])
        // single line comments
        System.out.println("Hello World");

When the above Java Program is compile and executed, it will produce the following output:

structure of java program

Let's explain the above Java Program:

  • public class JavaProgram: This line of code has three parts:
    • public : This is an access modifier keyword, tells the compiler to access the class. Various values of access modifiers are public, protected, private or default (no value).
    • class : This keyword is used to declare the class. Name of the class (here JavaProgram) followed by this keyword.
    • JavaProgram : This is the name of the class. You can give this name according to your demand/program.
  • Comment Section : You can write comments in two ways:
    • Single Line Comments : It start with two forward slashes i.e. // and continue to the end of the current line. Line comments do not require an ending symbol.
    • Multi Line Comments : Also called as block comments start with a forward slash and an asterisk (/*) and end with an asterisk and a forward slash (*/).Block comments can also extend across as many lines as needed.
  • public static void main (String args[]) : This line of code has three keywords, main, and string arguments:
    • public : Access Modifier
    • static : static is reserved keyword which means that a method is accessible and usable even though no objects of the class exist.
    • void : This keyword declares nothing would be returned from method. Method can return any primitive or object
    • main : This is the main where program starts running or from here our compiler starts checking and running our java program/code.
    • Arguments : This is for the arguments purposes which will be included inside the curly bracket.
  • System.out.println("Hello World"): This line of code has also four parts:
    • System : It is the name of Java utility class.
    • out : It is an object which belongs to System class used in sending the data which is inside the bracket to the console.
    • println : It is print line which is utility method name which is used to send any String to console.
    • Hello World : It is String literal set as argument to println method


Let's look at some more Java program. Here is the first program:

/* Java Program Structure Example 1 */

public class JavaProgram
    public static void main(String args[])
        System.out.println("Hello Compiler, This is simple Java program");

When the above Java program is compile and executed, it will produce the following output:

java program structure

Here is another program in Java, takes input from the user:

/* Java Program Structure Example 2 */

import java.util.Scanner;

public class JavaProgram
    public static void main(String args[])
        String name;
        Scanner scan = new Scanner(;
        System.out.print("Enter your Name :");
        name = scan.nextLine();
        System.out.println("Hello " +name+ ", this is codescracker");

When the above program is compile and executed, it will produce the following output:

program structure of java

You can go check to many Java programs at Java Programming Examples.

Java Online Test

« Previous Tutorial Next Tutorial »