Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java Object & Class

Object

Object is an actual physical existence of a Class. It is a real entity whose structure, identity and behavior are decided by a Class.

Class

Class is just known as a template or a blueprint for an object. It is used to decide structure, identity, and behavior of an object.

It can also be considered as a mold for an object.

Java Programming Language Class and Object

Syntax of Java class

 class ClassName
{
    variables;
    methods;
}

Variables & methods are called members of the class.
Variables may be the instance variables or static/class variables.
Similarly, methods may be instance methods or static/class methods.

Example of class & its object

class Circle 
{
     int radius;  // instance variable
	
     public static void main(String args[])   // method
     {
         Circle c1 = new Circle();
         Circle c2 = new Circle();
         c1.radius = 3;
         c2.radius = 5;
         double area;
         area = 3.14*c1.radius*c1.radius;
         System.out.println("Area of Circle c1 = "+area);
         area = 3.14*c2.radius*c2.radius;
         System.out.println("Area of Circle c2 = "+area);
     }
}

Output

Area of Circle c1 = 28.259999999999998
Area of Circle c2 = 78.5

Here, every object/instance of class Circle contains a separate copy of variable radius. Therefore variable radius is called instance variable.
The c1 & c2 are the references to the objects of type Circle.
The c1 points to one object & c2 to another.

Java Programming Language reference & Object

Changes made to one instance variable do not affect other instance variables.
If access modifier is not used with a class member then by default, the member of a class is public within the same package. Member without the modifier cannot be accessed from outside its package.
It can be accessed only from the same class or other classes present in the same package.

Above program can also be written as below

class Circle 
{
    int radius;
}

class CalCircleArea 
{
    public static void main(String args[])
    {
        Circle c1 = new Circle();
        Circle c2 = new Circle();
        c1.radius = 3;
        c2.radius = 5;
        double area;
        area = 3.14*c1.radius*c1.radius;
        System.out.println("Area of Circle c1 = "+area);
        area = 3.14*c2.radius*c2.radius;
        System.out.println("Area of Circle c2 = "+area);
    }
}

Output

Area of Circle c1 = 28.259999999999998
Area of Circle c2 = 78.5

It is not compulsory for a Java class to have a main() method.
Because in Java application main() method is used only in the class that is the starting point of the application.
In the above program, class CalCircleArea is considered as an entry point therefore main() method is written in class CalCircleArea.
This is the standard way of writing Java programs.
Henceforth we’ll try to write a program in this format wherever it is necessary.

Object declaration

In the above program, lines responsible for object creation are

Circle c1 = new Circle();
           &
Circle c2 = new Circle();

These lines can also be written as

Circle c1;
c1 = new Circle();
          &
Circle c2;
C2 = new Circle();

These lines are responsible for object creation.
The c1 & c2 are just references to the objects of type Circle. That is, they point towards the object.
Note that code/line

Circle c1;

is not responsible for object creation.
The new keyword is responsible for object creation.

Note

In Java, we don’t know what does reference contains exactly.
It contains something like an address or a pointer.
But JVM knows how to make use of reference to get the right object.

Java Programming Language Object Declaration and its representation in Heap Memory

Therefore general syntax for an object creation is

ClassName reference = new ClassName();

reference – a reference to the object.
The new keyword allocates the memory for an object at runtime & returns reference to it.
The ClassName is the name of the class of which object is to be created.
Parentheses represent the constructor.

Multiple references to a single object

A single object can have multiple references as shown below:

Example

Circle c1 = new Circle();
Circle c2 = c1;
Circle c3 = c1;

Now each reference points to the same object.
Therefore any changes made to Circle object through reference c2 & c3 will affect the object to which c1 is referring.

Example

class Circle 
{
    int radius;
}

class MulRefSingObject 
{
    public static void main(String args[]) 
    {
        Circle c1 = new Circle();
        Circle c2 = c1;
        Circle c3 = c1;
		
        c1.radius = 5;
        System.out.println("c1 Radius :: "+c1.radius);
		
        c2.radius = 7;  // radius changed using c2
        System.out.println("c1 Radius :: "+c1.radius);
		
        c3.radius = 9;  // radius changed using c3
        System.out.println("c1 Radius :: "+c1.radius);
    }
}

Output

c1 Radius :: 5
c1 Radius :: 7
c1 Radius :: 9

 

Leave a reply