Java Program Structure6 min read


In this tutorial, we are going to discuss the Java program structure. Java is a popular programming language because it is object-oriented, platform-independent, and secure. We can create a wide range of apps using the Java programming language. So, before digging in, it’s important to first grasp the basic structure of a Java program. A complete java code is inside the class.

The sample structure of the Java program is given below. A program to display welcome message public class sample Public static void main(string[] args) { } We can type this program in a text editor like notepad. Member of class Javac The compiled program can be run using the java command.

System.out println ("Welcome to Java");

The name of the program must be the same as the class that contains the code and the extension must be Java. For example, the name of the file containing the above code must be The compilation is the process of converting source code to machine comprehensible form. A Java program is compiled using the command javac. The above code can be compiled using the command.

Points to remember

  • Type of Method – Void means not returning any value.
  • System – The class System is defined in package java.lang.
    • The run time environment automatically creates the object name System.
  • Object – Out It is an object within the system class. It is created as part of the creation of the object System.
  • Method – Println It is a method (function) associated with class PrintStream.
    • This method writes a single string parameter to output with a new line appended.
    • Print() and println()
    • print() and println() both are the methods of class printStream.
    • println() method appends a new line to the output string i.e. it outputs a carriage return after it outputs the specified parameter value.
    • The system.out.println without parameter outputs a carriage return i.e. it leaves a blank line.

Package in Java

A package is a collection of classes with the same name. You can name your package whatever you want. That is, if you wish to declare multiple classes in a single element, you can do it in a package. It is an optional element of the java program structure, which means that if you do not wish to declare any packages, there will be no issues and no problems. The keyword package is used to inform the compiler that a package has been built. Here, the package is a keyword that tells the compiler that the package has been created. It is declared as:

package package_name;

Class in Java

A Java program can have many class definitions. A class in Java can be defined as a program’s blueprint. It includes data on user-defined methods, variables, and constants. The main() method is found in at least one class in every Java program. We can’t write a Java program without the class. To define the class, we utilize the class keyword.

The Class Declaration

The above diagram shows that two primary components make up the implementation of a class: the class declaration and the class body. The class declaration declares the name of the class along with other attributes. The class declaration for the Stack class is fairly simple and indicates that the class is public and that its name is Stack. Often, a minimal class declaration such as this one is all you’ll need.

The Class Body

The class body follows the class declaration and is embedded within curly braces {and). The class body contains declarations for all instance variables and class variables (known collectively as member variables) for the class. In addition, the class body contains declarations and implementations for all instance methods and class methods (known collectively as methods) for the class.

Creating a Class

Here is a sample code for a possible implementation of a Bicycle class, to give you an overview of a class declaration. For the moment, don’t concern yourself with the details.

public class Bicycle{
    // the Bicycle class has three fields
	public int cadence;
	public int gear;
	public int speed;
	// The Bicycle class has a method
	public void setCadence (int c) {
    	cadence = c;

Object in Java

  • An object is a collection of data created and laid off according to the specifications of a class.
  • Object entirely consists of data and the code associated with the class. The object is an instance of the class.
  • The code associated with class stays where the class loader put it, it is not replicated within each object.
  • In Java, all class objects must be dynamically allocated.
Java program structure
Objects in Java


class Sample {
	int a
	double b; 

public class Demo { 
	Public static void main(String[] args) {
		Sample s = new Sample();

If we refer to this variable, it will produce a compile-time error.

And the following statement allocates an actual object and assigns a reference to it to s. Now, this can be used to access members. The object holds the memory address of the actual sample object.

Method in Java

Class consists of class member variables and class member functions. The class member functions are known as methods.

The general form of method is

type Method_Name (parameter_list)
// body of method
  • The type of method is noting but the type of value the method returns.
  • If any method is not returning any value then its type is void.
  • The method having return type (not void) returns value to calling function using return statement.
  • The general from of return statement is return value;

Example :

1) Sample Method

Void display ( )
system.out.println("Best Luck");


2) Method with arguments

Void average (int x, int y, int z)
double avg = (x+y+z) / 3.0 system.out.print("Average is");

3) Method with argument and return type

double average (int x, int y, int z)
Double avg = (x+y+z) /3.0
Return avg;

Main Method

The main() method is defined in this section. It is an important section in the java program structure. Because the main() function is where all Java applications begin to run. In other words, it is the class’s starting point. It has to be within the class. We construct objects and invoke methods in the main method. The main() method is defined using the following statement:

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

Implementing Methods

As you know, objects have behavior that is implemented by their methods. Other objects can ask an object to do something by invoking its methods. In Java, you define a class’s methods in the body of the class for which the method implements some behavior. Typically, you declare a class’s methods after its variables in the class body although this is not required.


Leave a Reply