Syntax of java program and its core elements


You are new with java, check out this article this will take you through the syntax of the java and this will explaing the core elements of the java syntax.

class Demo{
    private int a; //variable declaration and definition
    
    public Demo(int a){
        //defining constructor
        this.a = a;
    }
    
    private void printA(){
        System.out.println("the variable a contains value: "+a);
    }
    
    public static void main(String[] args){ //main method to start the
        Demo d = new Demo(10); //creating an Object
        d.printA(); //calling method
    }
}

This is the simplest structure of the Java program. This could have been more simpler but I decided to start ahead of our old friend "hello world !". Anyway, let us discuss what this small program tell us about the Java program structure.

First of all the Java is pure OOP so everything is in class and object. We define the class using the 'class' keyword. the syntax is
class ClassName{ //class body }

The Camel casing is not a default rule for the naming convention of Java class but is a convention used by the community to keep things identifiable.

In Class we make The first letter and first letter of every word capital. But in variable and methods we make first letter small and other words in the name capital.

private int a;

defines a variable or type integer. similarly you can define more data type.

syntax is <scope define> <type> <identifier>.

you can define the types in byte, short, int, char, long, float, double, boolean types.

public Demo(int a){

//defining constructor

}

The code above is a constructor, the constructor is used to define the object when called, and can be used to define or initialize something in the program. as you can see the name of constructor is same as name of the class for which it is defined. In example is a parameterized constructor but it can be left blank too, like


Demo(){
//constructor body
}

next a method is defined there are different concepts in java about methods and constructors and even classes. however the below structure is a simple one.


private void printA(){
//method body
        System.out.println("the variable a contains value: "+a);
}

The method syntax is simple, first the scope-identifier which is global. second is the return type it could be any object type or primitive type if there is not any required you can define it “void”. the body is defined in the block.

System.out.println("the variable a contains value: "+a);

the line above is used to print on the console. The System class defines the system stream and out is a predefined object on which we can call the println() method that will put the output on the console.


public static void main(String[] args){ //main method to start the
        Demo d = new Demo(10); //creating an Object
        d.printA(); //calling method
 }

Finally we are in main method. This is the entry point for any application you are writing in java.

this has a predefined form,


public static void main(String[] arg){
//main body
}

“public” makes sure the main method is accessible from anywhere.

“static” makes sure this is callable without any reference.

“void” means there is no return type

“main(String[] arg)” the name and parameter of method. The parameter String[] args is a trying type array. which is passed from command line.

Demo d = new Demo(10); //creating an Object

d.printA(); //calling method

These two likes are making object and calling the method of the class using that object.

the object “d” is of type “Demo” and is storing the object reference of the Demo class.

the next line is calling “printA” method on the “d” which is an object of “Demo” class.

This is the core structure of the java program.