Structure of a Java program

  • Last Updated: August 17, 2023
  • By: javahandson
  • Series
img

Structure of a Java program

In this article, we are going to learn about the structure of a Java program with basic examples.

 

A Java developer has to follow a certain structure to create a Java program. This structure is defined by the language. If we do not follow this structure then we will not be able to create a Java program and we will get exceptions. Below is a standard format to develop a Java program.

package <package-name>;
import statements;

class <class-name> {

   // Data members
   // User defined methods
   
   public static void main( String[] args ) {
      // Block of statements
   }
} 

Points to remember

1. The main method is the starting point of any Java application. Execution of the application starts from here.

2. In a Java application, there will be many Classes but only one Class will have the main method from where the execution begins.

Java program blocks

Package Statement

A Package is a collection of Classes, Interfaces and Sub packages. We have to mention the package information at the very top. The package helps us to group the Classes, Interfaces and Sub packages under one roof.

Ex. All the security related Classes are placed under java.security.* package ( java.security.* is a predefined package )

All the language-specific Classes are placed under java.lang.* package ( java.lang.* is a predefined package )

Note* java.lang.* is the default package that gets imported to every Java program by default.

In the same way, we can also create user-defined packages and group all the related Classes in one package. Package information tells us the Class that we are going to create belongs to what package.

package com.javahandson.pkg1;

public class Student {
    String studentName;
    public String getStudentName() {
        return studentName;
    }
    public void setStudentName(String studentName) {
        this.studentName = studentName;
    }
}

In the above example, Student Class belongs to the com.javahandson.pkg1 package.

Import Statement

An import statement is used to import different Classes and Interfaces in the current Class. We import the Class so as to utilize the functionality of that Class.

In the below example, we will import the above Student Class in the School Class

package com.javahandson.pkg;
import com.javahandson.pkg1.Student;

public class School {
    public static void main(String[] args) {
        Student student = new Student();
        student.setStudentName("Rohit");
        System.out.println(student.getStudentName());
    }
}

Output : Rohit

Here School Class is using the Student functionality hence we have imported the Student Class into School Class.

Import statements also help to solve the ambiguity if the Class names are the same.

Say we have 2 Student Classes One in the com.javahandson.pkg1 package ( which we have seen above ) and the other in the com.javahandson.pkg2 package.

package com.javahandson.pkg2;

public class Student {
    int studentId;
    public int getStudentId() {
        return studentId;
    }
    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }
}

Now which Student Class to import in School Class will be decided based on the import package name. In the above example, we have imported Student Class from com.javahandson.pkg1. Now we will import Student Class from com.javahandson.pkg2.

package com.javahandson.pkg;
import com.javahandson.pkg2.Student;

public class School {
    public static void main(String[] args) {
        Student student = new Student();
        student.setStudentId(101);
        System.out.println(student.getStudentId());
    }
}
Output : 101

We cannot import both the Student Classes in a Single Class. We will get a compile time exception.

package com.javahandson.pkg;
import com.javahandson.pkg2.Student;
import com.javahandson.pkg1.Student; // Exception will be raised here

public class School {
    public static void main(String[] args) {
        Student student = new Student();
        student.setStudentId(101);
        System.out.println(student.getStudentId());
    }
}

Output : java: a type with the same simple name is already defined by the single-type-import of com.javahandson.pkg2.Student

Class

Every Java program must start with the concept of Class. Class is an encapsulation of data members and their related methods. To know more about the Class please refer to the article principles of object-oriented programming

Data members and methods

Data members are variables or constants that we use to store some values.

Using data methods we can perform some logical operations or do some setter and getter operations.

Data members and methods can be of instance or static type.

public class Student {
    int studentId;

    public int getStudentId() {
        return studentId;
    }

    public void setStudentId(int studentId) {
        this.studentId = studentId;
    }

    public void study() {
        System.out.println("Student has to perform the study operation");
    }

    public static void main(String[] args) {
        Student student = new Student();
        student.setStudentId(101);
        System.out.println("Student id is : "+student.getStudentId());
        student.study();
    }
}

Output : Student id is : 101
         Student has to perform the study operation

Here studentId is the data member.

getStudentId is the getter method
setStudentId is the setter method
study is the method that performs some logical operation

Explaining the main method

1. Java program execution starts from the main method.

2. The return type of the main method is void because it will not return any value. JVM is calling this main method so there is no point in returning any output to JVM.

3. The access modifier of the main method is static because it will execute only once in the lifetime of a Java program. Also when the program starts we do not have any Object of the Class. If the main method was an instance method then we would need an Object to call this method that is the reason it is kept as static so that it can be called directly from JVM when the Class loads.

4. The access modifier of the main method is public because it can be accessed by all the Classes in the package as well as outside of the package. As we know JVM is a program and JVM has to call the main method which we have created so as to start the execution. If the main method is not public then JVM Class will not be able to call this main method hence the main method should be public.

5. The main method accepts an array of Strings as the argument. If we do not provide any arguments then it is empty but not null.

6. The block of statements inside the main method is the executable statements that perform some operations or it calls some user-defined methods.

public class Test {
    public static void main(String[] args) {
        System.out.println("Argument length - "+args.length);
    }
}

> javac Test.java

> java Test

Output: Argument length – 0

> java Test “Hello” “Developer”

Output: Argument length – 2

So this is all about the Structure of a Java program. I hope you liked this article. If you have any questions on this topic please add it below in the comment section. Thank you for reading this article.

Leave a Comment