Java Program Structure

Java Program Structure, Java Environment setup, Java Syntax, Java Language Fundamentals, Writing Java statements, and writing Java code blocks.

Java Program Structure

Sections of Java Program

1. Documentation Section – Optional

2. Package Statement – Mandatory

3. Import Statement/s – Depends on our program requirement

4. Interface Section //Optional

5. Class Definition { //Mandatory

6. main method(){
//Main Program contains normal statements and code blocks

Object Creation //Depends on our program requirement

Declarations/Initialization/Reading – normal statements

Operations, Display statements, etc…

Code Blocks, Conditions/Decision-making blocks, Loop Blocks, Constructor Block., Etc…


1. Documentation Section

It includes comments to tell the program’s purpose, it improves the readability of the Program

2. Package Statement

The package statement identifies the package that a Java program belongs to. If your program does not include a package statement, the program belongs to the default package, which is simply a package that has no name.

Example: package abcd;

3. Import statement/s

We import predefined and user-defined libraries using the “import” keyword


import java.util.Scanner;
java – Project
util – Package
Scanner – Class

import java.util.*; // Importing all classes from a package

4. Interface Section

It is an optional section. We can create an interface in this section if required. We use the interface keyword to create an interface. An interface is slightly different from the class. It contains only constants and method declarations.

5. Class Definition

public class Sample{


6. main() Method

Java program execution starts from the main() method, which is mandatory in every Java program.

public static void main (String [] args){

public – Access Modifier
static – Non Access Modifier (use main method without invoking an object)
void – Returns Nothing
main – Predefined Identifier(Method name)
//Within Main method

main method{
//Main Program
Creating Object/s
ClassName objectName = new ClassName();

Declarations….(Variable with Data Types, Constants/Final Variables)
Normal Statements
int a;//Declaration
int b=a;//Reading
System.out.println(10+20); //Print Statement
final int x=100;

if (){ //Condition Code Block

for (){ //Loop Code Block


Java Syntax and Program Structure

Program Example

//This program is for explaining Java Syntax and program Structure.

package abcd;

import java.util.Scanner;

public class Sample {
//Create a Non static method with arguments and return a value
public int add(int a, int b) {
int result;
return result;
//Create a Non static method without arguments and returns Nothing
public void sub() {
int x=100;
int y=50;
//Create a Static method with arguments and return a value
public static int multiply(int num1, int num2) {
int result= num1*num2;
return result;
//Create a Static method without arguments and returns Nothing
public static void comparision() {
int x=100, y=50;

if (x>y) {
System.out.println(“X is Big Number”);
else {
System.out.println(“Y is Big Number”);

public static void main (String [] args){
Scanner scan = new Scanner (;

System.out.println(“Enter Your Name”);
String myName = scan.nextLine();
System.out.println(“My Name is: ” + myName);
int a; //Variable Declaration
int b=200; // Variable Declaration with Initialization
char x=’Y’;
String myCountry = “India”;
int c, d, e; //Declaring multiple variables
int f=20, g=40, h=60; //Declaring multiple variables with Initialization
final int price=100;

if (a>b)
System.out.println(“A is Big Number”);
System.out.println(“B is Big Number”);

for (int i=1; i<=10; i++)
{ // Print 1 to 10 Numbers

int i=1;
while (i<=5) {
System.out.println(“My Output”);
//Print 1 to 5 Numbers except 4
for (int j=1; j<=5; j++) {
if (j!=4) {

//Create Object and call nonstatic methods
Sample obj = new Sample();
int val = obj.add(100, 200);

System.out.println(obj.add(10, 20));//30


//Call Static methods

val = multiply(10, 40);

comparision();//X is Big Number

Java Programming Language Syllabus

Java Step by Step Videos

Follow me on social media: