Java Program Structure: Packages, Classes & Methods

HelloWorld Java Program

// Java program to print "Hello, World!"
import java.lang.System;
public class HelloWorld
{
public static void main(String[] args)
    {
          // TODO Auto-generated method stub
          System.out.println("Hello World");
      }
}

Save this program as HelloWorld.java. This gathers the Java compiler and compiles the HelloWorld.java source file, creating the HelloWorld.class binary file. 

Java Program Structure

Java programs are built from classes and interfaces. A class defines a set of data structures, called variables, and the operations, referred to as methods, that are permitted on the variables. An interface specifies a set of methods that a class must implement.

The above HelloWorld program was built from the HelloWorld class. It also uses the System class.

Classes and interfaces are organized into .java files that are separately compiled. These .java files are called compilation units. The HelloWorld.java file you created with your text editor and compiled using javac is an example of a compilation unit.

The classes and interfaces contained in compilation units are organized into packages. Packages keep related classes and interfaces together so that names don’t clash. Our HelloWorld class is in the package. The System class, referenced by our program, is in the java.lang package.

Importing Packages (import Keyword)

The package statement identifies which package a compilation unit is in. The package statement must be the first in a compilation unit.

Syntax

package packageName;

Example:

package Hello;

If a compilation unit does not contain a package statement, the classes and interfaces contained in the compilation unit are put into a default package—the package with no name. This default package is the same for all classes within a particular directory.

The import Statement

The java.lang.System class is used to display Hello World!. The System class is in the java.lang package. To tell the compiler to use the System class in the java.lang package (as opposed to the System class in another package), import the System class using the import statement. Importing a class tells the compiler to use that class when it compiles your source code file.

Syntax

import fullClassName;

The class name supplied with the import statement must be a fully qualified class name. This means that the name of the package containing the class must be prepended to the name of the class.

Example:

The following import statement imports the System class from java.lang:

import java.lang.System;

The * wildcard character can be used instead of the class name in the import statement. It indicates that all classes in the package should be imported.

Example:

The following import statement imports all classes in the java.lang package:

import java.lang.*;

An alternative to using the import statement is to prefix the name of a class with its package name

java.lang.System.out.println("Hello World!");

Classes and Methods

The HelloWorld program was built on the HelloWorld class. This class is declared beginning with

class HelloWorld {

The class declaration ends with the last brace (}) of the file. HelloWorldApp declares and implements one method—the main method:

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

When a class is run from the command line with the Java interpreter, the main method is the one that is called.

The main method is always defined as public (that is, publicly accessible), static (applying to the class as a whole), and in the case of HelloWorld, void (no return value). The args[] parameter of the main method is defined as an array of String objects. The args[] parameter is used to pass command-line arguments to the main method.

The following statement represents the main method’s implementation:

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

This statement executes the println method of the object referred to by the out variable of the System class. The println method is executed using the “Hello World!” parameter. This command is what causes Hello World! to be displayed on your console window.

Scroll to Top
Verified by MonsterInsights