Basic Elements of a Java Program
A java program consists of the following elements.
Class description with access specifier and name
Comments (Optional)
Driver method
Statements
The figure below shows the basic structure of a Java program.
In the example, Hello is the name of the class. In Java, all code should be placed inside a class declaration. A class declaration is delineated by { }.
In following figure, the class uses an access specifier public, which indicates that the class is accessible to other classes from other packages. Packages are a collection of classes.
The next line in the figure below contains a curly brace { indicates the start of a block.
The figure below shows the next three lines, indicating a multiple-line Java comment.
The highlighted part in figure below indicates the name of one method in the Hello class, which is the main method. The main method is the starting point of a Java program. To successfully execute the program, make sure to follow the exact signature of the main method. The lines are delineated by /**, and */ are Java comments.
The next highlighted line in the following figure shows a single-line Java comment.
The command System.out.println() in the following figure is a statement that prints the text enclosed by quotation on the screen.
The last two lines in Figure 4.8, which contain the two curly braces } } are used to close the main method and class respectively.
Comments