Class and Object in java

In this page we will all Class and Object concepts.


Object in Java

Object is a instance of class, object has state and behaviors.
An Object in java has three characteristics:

1) State
2) Behavior
3) Identity

  State/Property/data: Represents data (value) of an object.

  Behavior/Methods: Represents the behavior (functionality) of an object such as deposit, withdraw etc.

  Identity: Object identity is typically implemented via a unique ID. The value of the ID is not visible to the external user. But,it is used internally by the JVM to identify each object uniquely.

Class in java

Class is a blueprint of an object which have certain properties and behavior. Class is template defined so that we can create the object of this class type.

Class is a logical entity.

No memory is allocated for class.

A class in java contain:

  Data Member/Data /fields

  Member Methods

  Constructors

  Blocks

  Nested Classes and Interfaces

Syntax to declare class


class Class_Name
{
//body of class
}

class keyword is used to create class in java.

Real life example of object and class

In real world many examples of object and class like dog, cat, and cow are belong to animal's class. Each object has state and behaviors. For example a dog has state:- color, name, height, age as well as behaviors:- barking, eating, and sleeping.

Simple Example of Creating Class and Object

In this example, we are going to create Student class that have two data members id and name. We are creating the object of the Student class by new keyword and printing the object values

Create Student Class.


class Student {
    int id;
    String name;
}

Create Class which contain main method so that we can create the object of Student class.


class Example {
    public static void main(String[] args) {
        //create the object of student class
        Student s = new Student();
        //assign values for s
        s.id = 34;
        s.name = "durgesh";
        //now printing the values of object
        System.out.println("ID :" + s.id);
        System.out.println("Name :" + s.name);
    }

}
ID :34
Name :durgesh

new keyword is used to create object of any class.

new keyword

new is a keyword which is used to dynamically allocate the memory for an objects at run time .Memory for objects are allocated at run time in Heap Area of Ram.

Let us take another simple example to understant class and object.

In this example we are going to create Person class which have name , id and address .

Create Person class and declare name , id and address


//creating student class
class Person {

    String name;//instance member
    int id;//instance member
    String address;//instance member

}

Create another class which contain main() method.



class Example {

    public static void main(String[] args) {
        //create the object of Person class
        Person p1 = new Person();
        //assign values for p1
        p1.name = "John";
        p1.id = 34343;
        p1.address = "USA";
        //now printing the values of object
        System.out.println("ID :" + p1.id);
        System.out.println("Name :" + p1.name);
        System.out.println("Address :" + p1.address);
        System.out.println("===============================");
        //creaet another object of Person class
        Person p2 = new Person();
        //assign values for p1
        p2.name = "Petter";
        p2.id = 3242;
        p2.address = "India";
        //now printing the values of object
        System.out.println("ID :" + p2.id);
        System.out.println("Name :" + p2.name);
        System.out.println("Address :" + p2.address);

    }

}
ID :34343
Name :John
Address :USA
===============================
ID :3242
Name :Petter
Address :India

We can also create these two classes in a single file .
Execute the class which contain main() method.

Understanding above example.

In the above example we have created 2 person object . so each of two person object have separate copy of instance variable.

Anonymous object

Anonymous simply means nameless. An object which has no reference variable is known as anonymous object. It can be used at the time of object creation only.

If you have to use an object only once, anonymous object is a good approach. For example:


new Calculation();//anonymous objec

Calling method using reference variable.


    Person c=new Person();  
    c.print();  

Calling method through anonymous object


new Person().print(5); 

Example of Anonymous Object creation.


class Person {

    public void print() {
        System.out.println("Hello....");
    }
    public static void main(String[] args) {
        new Person().print();//calling print method on anonymous object
    }
}
Hello....
woman avatar