Java Program basic Structure

In the lesson, we will look at the Java program basic structure we coded in the previous lesson. We will inspect the various components of our “Java Hello World” program and try to understand it more closely. Here is the program we coded in the last section for our reference:

Java Program basic Structure

package com.javdevjoural;

public class Main {
    public static void main(String[] args) {

        //print Hello World!!
        System.out.println("Hello World");

The first line in our program package; is known as java package. This line tells the compiler that our Main class belongs to the package. A package in a Java program is a grouping of related classes and interfaces. It’s a namespace similar to the different folders on your computer where you use them to manage your files.

Don’t worry about the classes or interfaces. We will cover them in more details in the later lessons.

1. Java Package

When we add the package statement to our program, we are essentially asking the compiler to include this file in the Package. Here are some important things to keep in mind:

  • Compiler will create a folder structure as per the package name (every . will create a new folder).
  • Compiler will save the class in to the folder. In our example the file will be saved in the <code>gettingstarted folder.
  • Java classes with the same package are stored in the same folder

If we navigate to the project in our computer, you will see the folder structure being created based on the package structure. This is how the package structure look like in my computer:

Java Program Basic Structure
Java Program Basic Structure

We got an understanding of the package in our java program basic structure. There are a few things which you should always keep in mind.

  • As a convention, we should use a lowercase for our package name. You can use upper cases, but it’s not recommended.
  • Java is a case-sensitive language, so gettingstarted and gettingStarted are not the same thing.

1.1. Advantages of Java Package.

Java package is a name space or just folder structure, than why do we need package in java? or what are the benefits of using a well-defined package name in Java programming? Well, there are many benefits of declaring package name in your Java class.

  • It prevents naming conflicts. You can define 2 or more classes with the same name in different packages.
  • Java packages are used to organize the same functionality in the package. Let’s say you are creating a calculator in Java, I will put all classes in addition package which helping to build addition feature for my calculator.
Did you know that Java also comes with many pre-created packages that contain code? E.g. Java 8 has introduced a new Date and Time API under java.time package and all classes related to time and date are part of this package.

2. The HelloWorld Class

Let’s move to the second important part of our Java program basic structure, it’s out HelloWorld class.We will talk more about classes in the later chapter, however in our example the HelloWorld class start at line 3 with an opening curly brace { and end with the matching closing curly brace }. Remember that these curly braces are used a lot in the java programming to show

public class HelloWorld {
  • Start and end of the class.
  • Start and end of the functions.
  • Start and end of code element.

Our HelloWorld class contains a main() method which is the entry point for JVM or simply the Java program always starts from the main() method.

3. Java main() method

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

The main() method is the entry point for any Java application. Whenever JVM runs an application, the main() method is the first method being called. Without main() method, we can’t execute our Java program. There are a few important points for the main method:

  • The main() method takes an array of String as input (see the String[] args in the main method)

Our example main() method is very simple, it’s contains only 2 lines with the first line as Java comment and the second line System.out.println("Hello World"); display the “Hello World” in the output.

Do you know all statements in Java ends with semi-colon(;), if you don’t add the semi-colon, you might see compile time error. It’s very similar to C++ or C language.


The last interesting part of the Java Program is the comment. They are used to providing more details or context to other developer. They are not used by compiler and are ignored during the compile time. Here is the Java comment example from our program.

 //print Hello World!!


In the lesson, we learned about the Java Program Basic Structure and its different building blocks. We will cover these section in more details during the course but this gives us good head start. As Always the entire source code for this series is available on out GitHub Repository.