Syntax And Elements of Declaration With Example In Java

The syntax in java refers to the set of rules defining how a Java program is written and interpreted. The syntax is mostly derived from C and C++.


Table of Contents

Elements Of Java Syntax:

A Java file can contain the following elements:

  • Package declaration
  • Import statements
  • Type declaration
  • Fields declaration
  • Methods declaration
  • Constructors declaration

Example of General JAVA Program Syntax:

Below is a program example that contains all of the above elements which will help you understand the basic syntax of a “.java file”:

package basicsyntax; //Package

import java.util.HashMap; //Importing Statements

//Class
public class MyFirstClass {

    String name = "Ram"; //Field

// Constructor in JAVA
    public MyFirstClass() {
}

//Method in JAVA
    public String getName()
    {
        return this.name;
    }

//Static Main Method in JAVA
    public static void main(String[] args) {

    }
}

Explanation of all the above elements are given below:

1. Package Declaration:

The first line of the program shows package declaration. This part, to be more specific:

package basicsyntax;

The package declaration consists of the word package, a space, and then the name of the package. The .java file should be located in a directory structure that matches the package name.

2. Import Statements:

A program can have single or multiple import statement depending on requirement. An import statement tells the Java compiler that this Java class is using other java class.

import java.util.HashMap;

3. Type Declaration:

Type declaration may be a class, an abstract class, an interface, an enum or an annotation.

Here below type declared is a class. The type declaration is beginning with opening brace ({)and ending with closing brace(}) like shown in the program:

public class MyFirstClass {
}

4. Field Declaration:

The field declaration ends with a semicolon(;). A type (class / interface / enum) can have more than one field.  Below is one field declaration:

String name = "Ram";

5. Constructors Declaration:

A constructor declaration is for initialization of value assigned to class variables. A class can have more than one constructor:

public MyClass() {
}

6. Methods Declaration:

When you create an instance of a class (an object) the object can have methods you can execute. These are also sometimes called “instance methods” because they require an instance of the object before you can call the method.  Here is the method declaration from the example above:

public String getName() {
return this.name;
}

7. Main Static Method:

A static method belongs to the class, not objects of the class which means that you can call a static method without having an object of the class the static method belongs to.

public static void main(String[] args) {

}

Importance Of Syntax:

  • Syntax helps you in using or calling the function even though you do not know the whole implementation inside it.
  • It helps the compiler in performing the cross check of the function called used in code, as it checks if the syntax matches with used one or not.
  • It also helps in solving compiler error to some extent.

Important Points To Remember:

Following points should be keep in mind about java programs:

  • Case Sensitivity: – Java is case sensitive, which means the uppercase and lowercase letters must be treated as different. For example, identifier Cat and cat would have different meaning in Java.
  • Class Names: – The first letter of a class name should be in Upper Case. If a class name has several words then each inner word’s first letter should be in Upper Case. For example: class MyFirstClass.
  • Method Names: – The first letter of a method name should be in Lower Case. If a method name has several words then each inner word’s first letter should be in Upper Case. For example:  public void myGetMethod()
  • Program File Name – The class name and the program file name should be exactly same. You should save program file using the class name (Remember Java is case sensitive) and add ‘.java’ to the end of the file name when saving it. (if the file name and the class name is not same your program will not compile)
    For example: Suppose ‘MyFirstProgram’ is the class name. Then the file should be saved as ‘MyFirstProgram.java’
  • public static void main(String args[]) –It is a mandatory part of every Java program that Java program processing starts from the main() method.