In this tutorial, you will learn the structure of a Java program, and different components in it, with an example.
Structure of a Java program
A typical Structure of a Java program would contain the following elements:
- Package declaration
- Import statements
- Class definition
- Class variables, Local variables
The below picture would show the above mentioned elements that make out the structure of Java program.
Classes in java could be placed in different directories/packages based on the module they are used in or the functionality it provides. For all classes that belong to a single parent source directory, path from source directory is considered as package declaration.
There would be classes written in other folders/packages of your working java project and also there are many classes written by individuals, companies, institutions, enthusiasts etc., which could be useful in our program. To use them in a class, we would need to import the class that we intend to use. Many classes could be imported in a single program and hence multiple import statements could be written.
A name should be given to class in java file. This name is used while creating an object of the class, in other classes/programs.
Variables are the means of storing the values of parameters that are required during execution of the program. Variables declared with modifiers have different scopes, which define the life of a variable. We shall see in detail about the modifiers like global, local, static and private at Variables in Java.
Execution of a java application starts from “main” method. In other words, its an entry point for the class or program that starts in Java Run-time.
This is a room for a set of instructions which form a purposeful functionality or a routine that would be required to run multiple times during the execution of the program. To not repeat the the same set of instructions when the same functionality is required, the instructions are enclosed in a method. A method’s behavior could be exploited by passing variable values to the method (also called arguments of a method).
In this Java Tutorial, we have learnt about the Structure of a Java program. In our next Java Tutorial, we shall see the Object Oriented Programming Concepts, starting with Classes and Objects in Java.