Open In App

Structure and Members of the Java Program

Last Updated : 12 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

When we are writing any program in any language, we need to follow a standard structure for writing the program which is recommended by the language experts. A Java program may contain many classes of which only one class will have a main method. The class will contain data members and methods that operate on the data members of the class. 

To write a Java program, we first need to define classes and then put them together. Generally, a standard Java program consists of the following blocks as shown in the below figure.

Sample Java Program

Explanation: 

  • 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 interfaces. In Java by default java.lang.* package is imported by every program. 
  • Class is a keyword used for developing user-defined data types. Every Java program must start with a prototype of the class. The class has been declared public, which means all classes can access the class from all packages. Generally, however, we will declare classes in Java without specifying a modifier. 
  • Class name is the name given to that class. Every class name is treated as one kind of user-defined data type. 
  • Data Members represent either instance members or static members. 
  • 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. 

Constructor initialization

When we create the object of the bird class as shown above: 

bird b = new bird(); 

Here, the new keyword 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 synchronized while methods can be declared. Constructors do not have return types while methods do. 

  • User-defined methods represent 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. 
  • All Java program starts their execution with the main() method, so the main() method is known as the backbone of the program. The Java Virtual Machine starts running any Java program by executing the main() method first. 
  • Java’s main() method is not returning any value so its return type must be void. 
  • Also, main() method executes only once throughout the life of the Java program and before the object creation so its nature must be static. 
    The main() method is accessed in all the Java programs, its access specifier must be public (universal). 
  • Each and every main() method of Java must take an array of objects of the String class as an argument. 
  • The block of statements is set of executable statements written for calling user-defined methods of the class. 
  • If we have multiple Java files then the naming convention of the class file in Java is that whichever class is containing the main() method, that class name will be given as the file name with an extension (dot) .java. 

Types of Data Members 

Java Class is a collection of data members and functions. Any Java program may contain two types of data members.

  • Instance or non-static data members
  •  Static or class data members 

Difference between Instance Data Members and Static Data Members:

Difference between Instance and Static Data Members

Types of Methods

In Java programs generally, we may define two types of methods apart from the constructor.

  •  Instance or non-static methods
  •  Static or class methods 

Difference between Instance Methods and Static Methods: 

Difference between Instance and Static Methods

The following example named TestGVP.java demonstrates the use of different members of the Java class. 

Java
// Java code to show structures and
// members of Java Program
public class classMember {

    // Static member
    static int staticNum = 0;

    // Instance member
    int instanceNum;

    /* below constructor increments the static
    number and initialize instance number */
    // Constructor method
    public classMember(int i)
    {
        instanceNum = i;
        staticNum++;
    }

    /* The show method display the value in the staticNum
     * and instanceNum */
    // instance method
    public void show()
    {
        System.out.println(
            "Value of Static Number is:" + staticNum
            + "\nValue of Instance number is:"
            + instanceNum);
    }

    // To find cube
    // Static method
    public static int cube()
    {
        return staticNum * staticNum * staticNum;
    }

    // Driver code
    public static void main(String args[])
    {
        classMember gvp1 = new classMember(2);
        System.out.println(
            "Value after gvp1 object creation: ");
        gvp1.show();

        classMember gvp2 = new classMember(4);
        System.out.println(
            "Value after gvp2 object creation: ");
        gvp2.show();

        // static method can be accessed by class name
        int cub = classMember.cube();
        System.out.println("Cube of the Static number is: "
                           + cub);
    }
}

Output:

Output Screen


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads