Object and Class in Java

Object is the physical as well as logical entity where as class is the only logical entity.

object in java

java oops concepts

An entity that has state and behavior is known as an object e.g. chair, bike, marker, pen, table, car etc. It can be physical or logical (tengible and intengible). The example of integible object is banking system.

An object has three characteristics:

  • State
  • Behavior
  • Identity

State: Represents data (value) of an object.

Behavior: 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 is also can be used to achieve user defined data types.

In real world many examples of object and class like dog, cat, and cow are belong to animals 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.

Class in java

Class: Class is a blue print which is containing only list of variables and method and no memory is allocated for them. A class is a group of objects that has common properties.

A class in java contains:

  • Data Member
  • Method
  • Constructor
  • Block
  • Class and Interface

Syntax to declare a class:

class <class_name>{  
    data member;  
    method;  
}  

Simple Example of Object and Class

In this example, we have created a Employee class that have two data members eid and ename. We are creating the object of the Employee class by new keyword and printing the objects value.

				 
class Employee
{
int eid;  //data member (also instance variable)
String ename;  //data member(also instance variable)

public static void main(String args[])
 { 
Employee e=new Employee();//creating an object of Employee
System.out.println(e.eid+" "+e.ename);
 }  
}  
Note: A new keyword is used to allocate memory at runtime, new keyword is used for create an object of class, later we discuss all the way for create an object of class.

Different ways to create object in Java

There are different ways to create object in Java listed below.

  • By new keyword
  • By newInstance() method
  • By factory method etc.
  • By clone() method

Anonymous Object

To create simply an object but not to hold this object in a reference variable, is known as Anonymous Object. Anonymous object is created when we need to use an object only once in a single line.

Example :

	
 class Calculate
{
   public int cube(int i)
    {
    return i*i*i;
    }
  public static void main(String ar[])
   {
    int c=new Calculate().cube(5); //creating anonymous object and call cube() method
    System.out.println("cube of 5 is : "+c);
    }
}
Output :

cube of 5 is : 125

Difference between Class and Object

Class Object
Class is a container which collection of variables and methods. object is a instance of class
No memory is allocated at the time of declaration Sufficient memory space will be allocated for all the variables of class at the time of declaration.
One class definition should exist only once in the program. For one class multiple objects can be created.