Parts of a Java Program

A deep introduction of java HelloWorld program.

A Java program contains different parts and we will examine the HelloWold Example that we have.

1. Class Comments
Class Comments can be placed on top most of the java file or right before the Access Modifier-Reference Type-Class Name section or both.

2. Package
Package is actually a folder where you store or put your java class file. The dot “.” in the package name represents the folder names separators. Note that you cannot use any java reserved words when naming your package.

3. Imports
The imports are the additional classes used in your class. Classes that are imported are the those that does not belong to java.lang.* package and those that are not in the same package as your class have.

4. Access Modifier, Reference Type and Class Name
public: is a Class Access Modifier and this determines if class is accessible to another class. Class access modifier have similar behavior with method’s access modifier.

class: is a Reference Type. class, interface, enum are the example of Reference Type.

HelloWorld: is the Class Name

5. Method Comments
These are the comments commonly found before the method declaration.

6. Default Method’s name, Access Modifier, return type and arguments,

An executable java program (*.jar) needs a default method to run.

In the Java programming language, every application must contain a main method whose signature is:

The modifiers public and static can be written in either order (public static or static public), but the convention is to use public static as shown above. You can name the argument anything you want, but most programmers choose “args” or “argv”.

The main method is similar to the main function in C and C++; it’s the entry point for your application and will subsequently invoke all the other methods required by your program.

The main method accepts a single argument: an array of elements of type String.

public static void main(String[] args)

This array is the mechanism through which the runtime system passes information to your application. For example:

java MyApp arg1 arg2

Each string in the array is called a command-line argument. Command-line arguments let users affect the operation of the application without recompiling it. For example, a sorting program might allow the user to specify that the data be sorted in descending order with this command-line argument:


The “Hello World!” application ignores its command-line arguments, but you should be aware of the fact that such arguments do exist.

Finally, the line:

System.out.println(“Hello World!”);

uses the System class from the core library to print the “Hello World!” message to standard output. Portions of this library (also known as the “Application Programming Interface”, or “API”) will be discussed throughout the remainder of the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *