Class and object in java with example is most important in object oriented programming language such as java.
In this tutorial we will see class in java, class in java with example, object in java and class and object in java with example program.
Class in java
What is class in java ?
Class in java definition : A class is a user defined data type which group data members and its associated functions together.
Data is encapsulated in a class by placing data fields and method inside body of the class definition.
Class Contains Data members and member function in it.
A class is a type of structure.
It is heterogeneous collection of elements.
Example
Class Fruits which can hold different type of fruits in it.
Syntax of class
class class_name{
data_type member1_name;
data_type member2_name;
…..
return_type method1(arguments)
{
//code
}
return_type method2(arguments)
{
//code
}
}
Syntax of class in java contains, class => keyword
class_name => name of the class
Data members => variables in class
Members functions => methods in class
How to write class in java ?
Following example shows the definition of class which contains data members and methods.
Class in java example
class Demo
{
int a;
void display()
{
System.out.print(a);
}
}
Object in java
Object is an entity.
An object can be anything a person, a place or thing.
What is object in java ?
In object oriented programming language objects are called as run time entity.
Example
Banana is object of type fruits.
How to create object in java ?
Creating object
Creating object is referred to as instantiating an object.
Objects of specific class are created using new operator which returns reference to that object.
A object in java is a variable which is assigned a object reference.
Classes create objects and objects use methods to communicate between them.
Instantiating object of class
class_name object_name;
object_name=new class_name;
Syntax to create an object in java
class_name object_name=new class_name();

The above diagram shows the creation of object. When we write Example e; it creates reference e of type Example and when we write e=new Example () it allocates memory for e object.
Object consist of
- State : Attributes of object
- Behavior : Methods/functionality of object
- Identity : Name of object (represent individual object)
Object example in java
class Demo
{
int a;
void disp(){ }
public static void main(String args[])
{
Demo d1=new Demo(); //creating object of class
Demo d2=new Demo(); //creating object of class
}
}

We can create any number of objects belonging to that class. Each object is associated with data of type class with which they are created.
Algorithm for class and object
- Start
- Write class
- Declare variables
- Define methods
- Write main class
- Next write main method
- Create object of class
- Call methods using object
- End
Class and object in java program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
class Demo{ int no; String name; Demo(int n, String nm){ name=nm; no=n; } void display() { System.out.println("No = "+no); System.out.println("Name = "+name); } } public class Main { public static void main(String[] args) { System.out.println("Class and object in java"); Demo ob=new Demo(1,"TechinicalSeek"); ob.display(); } } |
Output :

Explanation
1. Execution starts with the main method
2. First we create object of the class and pass values.
Demo ob=new Demo(1,”TechinicalSeek”); => it will call parameterized constructor
Demo(int n, String nm){ => constructor executed
name=nm; => name= Technicalseek
no=n; => no =1
} => returns to main
3. Now we call method using object
ob.display(); => calls method of class demo
void display() { => method executed
System.out.println(“No = “+no);
System.out.println(“Name = “+name);
} => returns to main
4. End with the program.
Leave a Reply