Structure of a Java program


Since Java is an object-oriented programming language, the development of a Java application begins with the creation of a class. The definition of the class is saved in a file with the same name and extension .java.

The structure and elements of the class are shown in the following screenshot,

package //package-name

import //package-name

public class //class-name {

	// variable declarations
	// constructor method(s)
	// other method definitions


The package statement

A package in Java is similar to a namespace that stores classes with similar functionality. The package keyword is used to indicate that the following text is the name of the package to which the class belongs. In Java, a class is always inside a package.

If no package is specified, the class belongs to the default package. Examples of built-in packages are java.swing,, etc. Classes related to GUI are stored in the java.swing package and classes related to I/O functionality are stored in the package and so on.

The import statement

The import statement is used to include packages and external classes in a Java class. To use classes from another package, you must import them with the import keyword. However, some packages are included by default in a Java program, namely java.lang.

You do not need to explicitly import them in order to use the classes from these packages. Also, you do not need to import classes that are in the same package, or classes that are used with the full name of their package. For example, f1 = new“myfile”);

The class declaration statement

A Java class declaration is identified by the class keyword followed by the class name. The use of the public keyword indicates the visibility of the class to other classes inside and outside the package.


Variables in a Java program represent attributes of an entity and are also called fields. Variable instances are those variables for which a class instance or object has its own copy of that variable.


Methods in a Java program represent behaviors or functions that an object must perform on data. Method instances can be called by creating a class object.


Constructors in a Java program are special methods with the same name as the class. They are called at object creation time and are usually used to initialize class data members.

You can use any text editor to create a Java program, for example Notepad. The main steps to create a Java program are as follows,

  • Create a class and save it with the .java extension.
  • Compile the .java file.
  • Execute the Java program.

You can use any text editor to create a Java program, for example Notepad. The basic steps to create a Java program are as follows,

public class Hello {
  public static void main(String[] args) {
    System.out.println(“Hello World ! ”);

The class declaration consists of an access modifier, the class keyword and the class name, followed by curly braces {}. Java is a tightly encapsulated language, so nothing can be placed outside the class. In the example, the class name is Hello.

Inside the class is the main() method, which is the entry point into the application, that is, the execution of every Java program begins with a call to the main() method.


public static void main(String[] args) {
  // code statements 

Below is a brief description of the important keywords of the program,

  • public: Allows the JVM to call the main() method.
  • static: Allows the JVM to call a class method without creating an instance of it.
  • void: Specifies the data type of the value returned by main() method. The void data type means that the method will not return any value.
  • args: An array of strings storing arguments passed on the command line.

In the main() method, the actual functionality of the program is specified using the System.out.println() statement. The println() method takes a String argument and outputs it to the console.

The System class is a predefined class in Java that allows access to system resources such as the console. The out keyword represents the output stream connected to the console. Save the file as

To save the program, click File > Save As in Notepad and type in the File Name field. Quotes prevent you from saving the file with the extension

Leave a Comment