Structure Of Java Program

Before proceed to learn Java, it is important to understand the basic structure of a Java program

Package List;
public class <Class name>
{ 
   Data members;
   Constructor functions;
   User-defined methods(); 

   public static void main(String args[])
    {
        Block of statements;
    }
}
 
 Explanation:
1. Package is a collection of classes, interfaces and sub packages. In a java program if we are using any pre-defined classes and interfaces then it is the responsibility of the java programmer to import that particular package containing such specific classes and interface. In java by default java.lang.* package is imported by every program.

2. Class is a keyword used for developing user defined data types. Every java program must starts with a prototype of class. The class has been declared public, means all classes can access the class from all packages. Generally, however, we will declare classes in java without specifying a modifier.

3. Class name is the name given to that class. Every class name is treated as one kind of user defined data type.

4. Data Members represents either instance members or static members.

5. Constructor function is called when an object of the class is created. It is a block of code that initializes the newly created object. The constructor simply has the same name as the name of the class name. A constructor does not have a return type. A constructor is called automatically when a new instance of an object is created. In the following code, the constructor bird() prints a message.



When we create the object of the bird class as shown above:
bird b = new bird();
The new keyword here creates the object of class bird and invokes the constructor to initialize this newly created object.
Constructor and method are different because the constructor is used to initialize the object of a class while the method is used to perform a task by implementing java code. Constructors cannot be declared as abstract, final, static and synchronised while methods can be declared. Constructors do not have return types while methods do.

6. User-defined methods represents either instance (or) static and they will be selected depends on the class name and these methods are used for performing the operations either once (or) repeatedly. All the user-defined methods of a class contain logic for a specific problem. These methods are known as Business logic methods.

7. All java program starts its execution with main() method so main() method is known as the backbone of the program. The Java Virtual Machine starts running any java program by executing main() method first.

8. Java’s main() method is not retuning any value so its return type must be void.

9. Also main() method executes only once throughout the life of java program and before the object creation so its nature must be static.

10. The main() method is accessed in all the java programs, its access specifier must be public (universal).

11. Each and every main() method of java must take an array of objects of String class as an argument.

12. The block of statements are set of executable statements written for calling user-defined methods of the class.

13. If we have multiple java files then the naming convention of class file in java is that, whichever class is containing main() method, that class name will be given as the file name with an extension (dot) .java.