Structure and Members of the Java Program

20 Aug, 2021
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. 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 following blocks as shown in below figure.
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 synchronized while methods can be declared. Constructors do not have return types while methods do. 6. 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. 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 returning any value so its return type must be void. 9. Also main() method executes only once throughout the life of the 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. Types of Data Members: Java Class is a collection of data members and functions. Any java program may contain two types of data members. They are; 1. Instance or non-static data members 2. Static or class data members The following table describes the difference between the two. 
Types of Methods: In java program generally we may define two types of methods apart from constructor. They are; 1. Instance or non –static methods 2. Static or class methods The following table describes the difference between the two. 
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 Programpublic class classMember{     // Static memberstatic int staticNum = 0; // Instance memberint instanceNum; /* below constructor increments the staticnumber and initialize instance number */public classMember(int i) //Constructor method{    instanceNum = i;    staticNum++;} /* The show method display the value in the staticNum and instanceNum */public void show() //instance method{    System.out.println("Value of Static Number is:" + staticNum +                        "\nValue of Instance number is:"+ instanceNum);} // To find cubepublic static int cube() //Static method{    return staticNum * staticNum * staticNum;} // Driver codepublic 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 :
VinodDesai
simranarora5sos
java-basics
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate any Map in Java
HashMap in Java with Examples
ArrayList in Java
Collections in Java
Stream In Java
Multidimensional Arrays in Java
Singleton Class in Java
Set in Java
Stack Class in Java
Initialize an ArrayList in Java