1
0

CLASS and its SYNTAX in Java

CLASS:

As we have discussed at the starting of this course that Java is a pure Object-Oriented Programming Language (OOPL) and the Java program structure starts with class only. In OOPL we wish to encapsulate classes by defining the state and behavior of data in the form of objects.

A class is essentially a description of how to make objects which contain Fields (Data Members) and Functions (Methods). It behaves like a basic data type int. Therefore, class is termed as user-defined data type and their variables are termed as objects. In Java, objects are also known as instances of class.

SYNTAX:

The basic structure of class is:

<class modifier> class <class-name> <extends clause> <implements clause> // class header

{
    <field declaration>;  // class body
    <method declaration>;
    <constructor declaration>;
    <initializer block>;
}

In the class header, the name of the class is preceded by a keyword class. In addition, the class header can specify the following clauses i.e.

  • scope or accessibility modifier (private, public, protected)
  • additional class modifier (final, abstract)
  • any class it extends
  • any interface it implements

The class body can contain a member declaration which comprises of:

  • field declaration
  • method declaration
  • constructor declaration and
  • initializer block

Following is an example of creating a class in Java and the name is FirstClass:

Program to demonstrate the structure of class

class FirstClass
{
    int age;
    double salary;
    void Data()
    {
        age=25;
        salary=12345.67;
    }
    void Show()
    {
        System.out.println("My Age = "+age);
        System.out.println("My Salary = "+salary);
    }
}

Above is an example of creating a class in Java and the class name is FirstClass. It declares two instance variables (fields) i.e. age and salary of int and double type respectively. We have also declared two methods i.e. Data() and Show(). Remember these instance variables and methods are only declared and thus no storage space has been created in memory. Memory to instance variables and instance methods will be allocated when objects of the class are created. Instance variables and methods are also known as member variables and member methods respectively.

Note: We must give a file name in accordance with a class that contains the main() method not according to the FirstClass in the above program.

Variable Declaration:

The general syntax of a variable declaration is:

  <data-type> <variable-name>;

Here, <data-type> can be any of the in-built data types or some existing class name. The data types are chosen according to the value you want to store. <variable-name> can be any general variable written according to variable naming rules.

Method Declaration:

The general syntax of method declaration is:

<method-modifier> <return-type> <method-name> (<formal-parameter-list>) <throws-clause>

            // method body
{
    <local variable declaration>;
    <local object declaration>;
    <statements>;
}

In addition to the name of the method, the method prototype can specify the following information:

  • Scope or accessibility modifier (private, public, protected)
  • additional method modifier (static, final, abstract)
  • the type of return value or void if the method does not return any value
  • a formal-parameter list
  • a checked exception is thrown by the method in a throw clause (discussed in later articles)
  • The formal-parameter list is a comma-separated list of parameters for passing information to the method when invoked by a method call. An empty parameters list must be specified with empty braces (). Each parameter is a simple variable declaration consisting of its type and name.

<parameter-modifier><type><variable-name>;

Here <parameter-modifier> is a word called as final (discussed later).

It is important to note here that the signature of the method comprises <method-name>  and the <formal-parameter> list only.

Here, the method body is a block containing the local declaration and the statements of the method.

Following is an example of creating methods in java:

In the above example of FirstClass, two methods Data() and Show() are used i.e.

void Data()
{
    age=25;
    salary=12345.67;
}
void Show()
{
    System.out.println("My Age = "+age);
    System.out.println("My Salary = "+salary);
}

The methods  Data() and Show() have a return type of void because they do not return any value. We didn’t pass any parameter to these methods which is why () empty braces are used for both methods.

Leave a Reply