Structure of Java Program

We can use any editor to write java programs. We can write in notepad also. But there are some good IDE (Integrated Development Environments) are present those are Net Beans , Eclipse , Intelli J or Visual Studio Code

Every Java proram consists of a main class in which the main function is to be defined. The file name of the proram must be same as the mail class name. The extension of the file is ".java". Java is case sensitive language hence we have give more concentration on case of the java literals used in the program. Every java program contains a main method which takes String array as argument.

In every java program the beginning of the program we write import statement.

import java.awt.*; // Import statement
import java.util.*; // Import statement

Then we have to write the class name with public as modifier and class is keyword then followed by class name. Here we have to remember that the file name of the program must same as the main class name.

public class ProgramDemo

In java variable and methods or functions are defined in class. Those variable are defined

int x; // Variable declaration
float f; // Variable declaration

The method or function definition can be done in class. The function is example is given in the below code block.

void addition ( ){ // Function definition
int a;
int b;
int c;
a = 20, b = 43;
c = a + b;
System.out.println("Sum :"+c);
}

In order to access the variables and functions of the class we require a class variable. Class variable is called as object. We have to create object for the class. The object can be created using new keyword.

ProgramDemo myObj = new ProgramDemo();

The class variables and functions are accessed by using the object.variable name or object.function name. When we access the function then the we say function is called. When a funciton is called then the function is executed and the corresponding task will done.

System.out.println("Class Variable :"+myObj.x);
myObj.addition();


Complete Java Program



Output