×
☰ See All Chapters

What is Class and Object in Java

Class is a user defined data type. Class is one among four user defined data types. Any concept you wish to implement in a Java program must be encapsulated within a class. In object oriented programming, we design a program using objects and classes.  Object is the physical entity whereas class is the logical entity. A class works as a template from which we create the objects. Usually beginners will get confuse between class and objects. But it is simple to understand, Class is like a template or blueprint from which we create the objects.  We can imagine blueprint of a building as class, building as an object. We can have many buildings of same blueprint. Likewise we can create many objects of same class. We can imagine a running ATM application as collection of objects, and program written for ATM application as collection of classes. Class is program that you write; you can see it in editor. When you create the object of class, instances of the class will be created in JVM which you cannot see.

Members of class

Class can have any of the below members:

  1. Data members/fields/field variables 

  2. Block 

  3. Methods 

  4. Constructor 

  5. Inner class 

java-class-0
 

We will study in detail about each member of a class in coming chapters.

A Simple Example for Class        

class Box {

        double width;

        double height;

        double depth;

}

Declaring or creating Objects using new keyword

Declaring Objects using new involves two steps.

  1. Declaring a variable of the class type. This variable does not define an object. Instead, it is simply a variable that can refer to an object. 

  2. Acquiring an actual, physical copy of the object and assign it to variable. We can do this using the new operator. The new operator dynamically allocates (that is, allocates at run time) memory for an object at run time and returns a reference to variable. This reference is, more or less, the address in memory of the object allocated by new. This reference is then stored in the variable. Thus, in Java, all class objects must be dynamically allocated. 

Declaring Objects using new has this general form

java-class-1
 

classVariable is a variable of the class type being created.

The ClassName is the name of the class that is being instantiated.

In the example BoxDemo these statements can be written as

java-class-2
 
java-class-3
 

The first line declares mybox as a reference to an object of type Box. After this line executes, mybox contains the value null, which indicates that it does not yet point to an actual object. Any attempt to use mybox at this point will result in a compile-time error. The next line allocates an actual object and assigns a reference to it to mybox. After the second line executes, you can use mybox as if it were a Box object. But in reality, mybox simply holds the memory address of the actual Box object. The effect of these two lines of code is depicted in Figure below.

java-class-4
 

Assigning a reference (reference variable) to another reference

Consider the following example

Box b1 = new Box();

Box b2 = b1;

 

First statement creates Box object and assigns the memory address of this Box object to b1 reference. Second statement creates reference b2 and contents present in b1 reference (memory address of  Box object) is assigned to b2 reference.

java-class-5
 

The assignment of b1 to b2 did not allocate any memory or copy any part of the original object. It simply makes b2 refer to the same object as b1. Thus, any changes made to either b1 or b2 will affect both.

Although b1 and b2 both refer to the same object, they are not linked in any other way. For example, a subsequent assignment to b1 will simply unhook b1 from the original object without affecting the object or affecting b2.

For example:

 

Box b1 = new Box();

Box b2 = b1;

// ...

b1 = null;

Here, b1 has been set to null, but b2 still points to the original object.

Accessing Members of a class

To access members of class variables, we will use the dot (.) operator. The dot operator links the name of the object with the name of an instance variable. For example, to assign the width variable of mybox the value 100, you would use the following statement:

mybox.width = 100;

Example :

class Box {

        double width;

        double height;

        double depth;

}

 

class BoxDemo {

        public static void main(String args[]) {

                Box mybox1 = new Box();

                Box mybox2 = new Box();

                double vol;

                mybox1.width = 10;

                mybox1.height = 20;

                mybox1.depth = 15;

                mybox2.width = 3;

                mybox2.height = 6;

                mybox2.depth = 9;

                vol = mybox1.width * mybox1.height * mybox1.depth;

                System.out.println("Volume is " + vol);

                vol = mybox2.width * mybox2.height * mybox2.depth;

                System.out.println("Volume is " + vol);

        }

}

Creating multiple objects of same type

We can create multiple objects of same type as we do in case of primitives.

Example:

package com.java4coding;

 

class Box {

        double width;

        double height;

        double depth;

}

 

class BoxDemo {

        public static void main(String args[]) {

                Box mybox1 = new Box(), mybox2 = new Box();// Creating multiple objects of same type

                double vol;

                mybox1.width = 10;

                mybox1.height = 20;

                mybox1.depth = 15;

                mybox2.width = 3;

                mybox2.height = 6;

                mybox2.depth = 9;

                vol = mybox1.width * mybox1.height * mybox1.depth;

                System.out.println("Volume is " + vol);

                vol = mybox2.width * mybox2.height * mybox2.depth;

                System.out.println("Volume is " + vol);

        }

}

 


All Chapters
Author