Java Classes, Objects, Fields and Methods

A class is nothing but a blueprint or a template for creating different objects which defines its properties and behaviors.

An object is an instance of a class created using a new operator. The new operator returns a reference to a new instance of a class. This reference can be assigned to a reference variable of the class. The process of creating objects from a class is called instantiation. An object encapsulates state and behavior.

An object reference (or reference) provides a handle to an object that is created and stored in memory. In Java, objects can only be manipulated via references, which can be stored in variables.

Java class objects exhibit the properties and behaviors defined by its class. A class can contain fields (i.e. variables) and methods (i.e. functions) to describe the behavior of an object.

Methods are nothing but members of a class that provide a service for an object or perform some business logic. Java fields and method names are case sensitive. Current states of a class’s corresponding object are stored in the object’s instance fields. Methods define the operations that can be performed in Java programming.

A class has the following general syntax:

<class modifiers> class <class name> <extends clause> <implements clause>
//extends or implements clause is optional
{
    // Dealing with Classes (Class body)
    <field declarations (Static and Non-Static)>
    <method declarations (Static and Non-Static)>
    <Inner class declarations>
    <nested interface declarations>
    <constructor declarations>
    <Static initializer blocks>
}

 

Example 1

Below is an example of Objects and Classes related to the Rectangle class that defines two fields namely length and breadth. The class also contains two methods namely inputData() and computeArea().

AreaTest.java

class Rectangle {
    //fields
    int length, breadth; 

    //method 1
    void inputData(int l, int b) {
        length = l;
        breadth = b;
    }

    //method 2
    int computeArea() {
        return length * breadth;
    }
}

public class AreaTest {

    public static void main(String[] args) {
        Rectangle rect; //object reference of Rectangle class
        rect = new Rectangle(); //creating object of Rectangle class
        rect.inputData(20, 10); //calling method 1 using object reference 
        int area = rect.computeArea(); //calling method 2 using object reference 
        System.out.println("AREA = " + area);
    }
}

Output:

AREA = 200
 

Refer to a field / method:

This is accomplished by stating the name of the object reference, followed by a period (dot), followed by the name of the member inside the object like this:

objectReference.member 

We call a method for an object by naming the object followed by a period (dot), followed by the name of the method and its argument list, like this:

objectReference.methodName(arg1, arg2, ...)

For example:

rect.inputData(20, 10); 
int area = rect.computeArea();

 

Instance Fields and Instance Methods

An instance field (also called instance variable) stores the state of the object. Each class would have its own copy of the variable. Every object has a state that is determined by the values stored in the object. An object is said to have changed its state when one or more data values stored in the object have been modified. When an object responds to a message, it will usually perform an action, change its state etc. An object that has the ability to store values is often said to have persistence. The instance methods express the behaviors of the objects defined by its class. In the previous example the Rectangle class has two instance fields namely length and breadth and two instance methods named inputData() and computeArea().

 

Class Variables – Static Fields

We use class variables also known as Static fields when we want to share characteristics across all objects within a class. When we declare a field to be static, only a single instance of the associated variable is created common to all the objects of that class. Hence when one object changes the value of a class variable, it affects all objects of the class. We can access a class variable by using the name of the class, and not necessarily using a reference to an individual object within the class. Static variables can be accessed even though no objects of that class exist. It is declared using static keyword.

 

Class Methods – Static Methods

Class methods, similar to Class variables can be invoked without having an instance of the class. Class methods are often used to provide global functions for Java programs. For example, methods in the java.lang.Math package are class methods. We cannot call non-static methods from inside a static method.

Example 2

The Java program below showing the use of static fields and static methods.

CubeStaticTest.java

class Cube 
//static fields static int length = 10;  static int breadth = 10;  static int height = 10;

//static method
static int getVolume() {
       return length * breadth * height;
}
}

public class CubeStaticTest {

 public static void main(String args[])   {
System.out.println("Length = " + Cube.length);
 System.out.println("Volume = " + Cube.getVolume());

}
}

Output

Length = 10
Volume = 1000