Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

September 16, 2014 Categories: Core Java Basics. No Comments on Structure of a Java program

In this post I will explain the structure of a Java program. A Java program is a collection of one or more classes, in which, one class contains the main method. Before looking at the structure of a Java program, let’s see the structure of a Java class.


Structure of a Java class is as shown below:


Structure of a java class


A Java class can contain the following:


  • Package statement: A package statement is used to declare a Java class as a part of the specified package. More on declaring packages later. Package statement is optional. When we decide to write a package statement, it should be the first statement in the file. Only exception to this rule is writing comments.


  • Import statement(s)We can write one or more import statements. These are also optional. An import statement is used to link our class with other classes in the same package or other packages to use their functionality. More on import statement in other articles.


  • CommentsWe can write one or more comments in a Java class to explain the use of certain statements or provide extra information like purpose of the class, author’s name, date and time of creation etc. Comments are optional. Comments can be written in the first line or anywhere in the program. More on writing comments later.


  • Class declaration: A class declaration consists of the class keyword followed by the class name, which is followed by the body of the class represented using braces { }. A class declaration can contain the following:
    • Variable declarations
    • Comments
    • Constructors
    • Methods
    • Nested classes
    • Nested interfaces
    • Enumerations


All the above elements (variable declarations, comments etc…) can occur in any order.


Now, let’s look at the structure of a Java program. It will be as shown below:


structure of a java program


As I had already mentioned above, a Java program is a set of one or more classes in which each class will be declared following the structure of a Java class and one of the classes will have a main method to start the execution of the Java program.

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

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

Scroll Up