Java Program Structure

  • Java
  • 3 mins read

In this article, we discuss the basic structure of the Java program that is used for every Java console application.

Java Syntax

The Java Syntax refers to a set of rules that define the structure of the Java program. It is a combination of words and symbols that are considered to be correctly structured for the java program.

Class definition

Java is an Object Oriented Programming, so every Java program has at least one class (or interface). Each Java program has to be written inside a class as it is one of the main principles of Object-oriented programming that Java.

There can be multiple classes in a program. The first letter of every word in Name of the class (interface) should begin with an uppercase letter.

class HelloWorld
{ 
  // class definition
}

The above code defines a class named HelloWorld. HelloWorld is an identifier, you can use any valid java identifier.

main function

The main() function is the entry point for the execution of the java code. The name of main() is predefined and its code is user-defined. It is called/invoked by the Java Virtual Machine or JVM.

The main() method must be defined inside a class. We can call other functions and create objects inside this method. The main() method has the following syntax in java:

Syntax:

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

We can use [] after or before args, so we can also use

Syntax:

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

args is an identifier, we can use any valid java identifier also instead of args.

In java, main() must be public, static, and void and it must take an array of String as an argument. If we make any change in this, then the java program can be compiled but on execution, it throws an exception.

We describe the public, static, and void as follows:

public: The main() method must be public as it is invoked by JVM, which is outside the class. Remember that public members can be accessed everywhere.

static: The main() method must be static. The main() is called by JVM without creating the object. The static members can be called without creating the object.

void: the return type of main() must be void in java, as it does not return anything. Every method which does not have any return type is qualified with the void keyword.

The complete java program looks as follows:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test {
    public static void main(String[] args)
        throws IOException
    {
        // Enter data using BufferReader
        BufferedReader reader = new BufferedReader(
            new InputStreamReader(System.in));
 
        // Reading data using readLine
        String name = reader.readLine();
 
        // Printing the read line
        System.out.println(name);
    }
}

Note: In java8 and above versions we can define the main() method inside an interface also, as java8 allows us to define static methods and default methods.