An object is an instance of a class. Objects allow us to represent real-world entities such as students, employees, cars, books, and bank accounts in Java programs.
An object is created from a class and can contain its own instance data. It can also use the methods defined by its class.
For example, a Student object can have:
class Student {
String name = "Rahul";
int age = 20;
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
20
The common syntax is:
ClassName objectName = new ClassName();
Example:
Car car1 = new Car();
Explanation:
Car is the class name.car1 is a reference variable.new creates an object.Car() invokes a constructor.The reference variable allows us to access the object's accessible instance members.
class Car {
String brand;
String color;
void display() {
System.out.println(brand);
System.out.println(color);
}
public static void main(String[] args) {
Car car1 = new Car();
car1.brand = "Toyota";
car1.color = "White";
car1.display();
}
}
Toyota
White
The object is created first, its fields are assigned values, and then its method is called.
An object has state, behavior, and identity.
| Concept | Meaning | Example |
|---|---|---|
| State | Data stored in an object | Car color is red |
| Behavior | Actions performed by an object | Car starts |
| Identity | Distinguishes one object from another | A particular car object |
class Fan {
String color = "Blue";
void switchOn() {
System.out.println("Fan is running");
}
}
Here, color represents state and
switchOn() represents behavior.
We can create multiple objects from one class. Each object can store different instance field values.
class Student {
String name;
int age;
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = new Student();
s1.name = "Rahul";
s1.age = 20;
s2.name = "Priya";
s2.age = 21;
System.out.println(s1.name + " " + s1.age);
System.out.println(s2.name + " " + s2.age);
}
}
Rahul 20
Priya 21
Both objects come from the same class, but their instance field values are independent.
The dot operator (.) is used to access
accessible fields and call methods through an object
reference.
class Employee {
String name = "Amit";
int salary = 30000;
void showDetails() {
System.out.println(name);
System.out.println(salary);
}
public static void main(String[] args) {
Employee e1 = new Employee();
System.out.println(e1.name);
System.out.println(e1.salary);
e1.showDetails();
}
}
Amit
30000
Amit
30000
The expressions e1.name and
e1.showDetails() access the object's
field and method.
A constructor initializes an object when it is created. A parameterized constructor can receive values during object creation.
class Student {
String name;
int age;
Student(String n, int a) {
name = n;
age = a;
}
void display() {
System.out.println(name + " " + age);
}
public static void main(String[] args) {
Student s1 = new Student("Rahul", 20);
Student s2 = new Student("Priya", 21);
s1.display();
s2.display();
}
}
Rahul 20
Priya 21
Each constructor call initializes a separate object with the supplied values.
A reference variable refers to an object. Multiple reference variables can refer to the same object.
class Student {
String name = "Rahul";
public static void main(String[] args) {
Student s1 = new Student();
Student s2 = s1;
s2.name = "Amit";
System.out.println(s1.name);
System.out.println(s2.name);
}
}
Amit
Amit
s2 = s1 does not create
another object. Both references point to the same
object.
The == operator compares object
references to determine whether they refer to
the same object.
class Demo {
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
Demo d3 = d1;
System.out.println(d1 == d2);
System.out.println(d1 == d3);
}
}
false
true
d1 and d2 refer to
different objects. d1 and
d3 refer to the same object.
Instance fields receive default values when an object is created, unless other values are assigned.
| Field Type | Default Value |
|---|---|
int |
0 |
double |
0.0 |
boolean |
false |
char |
'\u0000' |
| Reference types | null |
class Demo {
int number;
boolean active;
String name;
public static void main(String[] args) {
Demo d1 = new Demo();
System.out.println(d1.number);
System.out.println(d1.active);
System.out.println(d1.name);
}
}
0
false
null
Local variables declared inside methods do not automatically receive these default values; they must be initialized before use.
A reference variable with the value
null does not refer to an object.
Calling an instance method through it causes a
NullPointerException.
class Student {
void display() {
System.out.println("Hello Student");
}
public static void main(String[] args) {
Student s1 = null;
// This causes NullPointerException:
// s1.display();
s1 = new Student();
s1.display();
}
}
Hello Student
class Book {
String title;
String author;
double price;
Book(String t, String a, double p) {
title = t;
author = a;
price = p;
}
void display() {
System.out.println("Title: " + title);
System.out.println("Author: " + author);
System.out.println("Price: " + price);
System.out.println();
}
public static void main(String[] args) {
Book b1 = new Book(
"Java Basics", "Amit Kumar", 350
);
Book b2 = new Book(
"Python Basics", "Neha Sharma", 400
);
b1.display();
b2.display();
}
}
Title: Java Basics
Author: Amit Kumar
Price: 350.0
Title: Python Basics
Author: Neha Sharma
Price: 400.0
Each Book object stores its own title, author, and price.
Student class and create
three student objects with different names.
Employee class with name
and salary fields. Display details for two employees.
Car class with brand, color,
and a method to display its details.
Book class with a
parameterized constructor.
Rectangle class with length
and width fields and a method to calculate area.
BankAccount class with an
account holder, balance, deposit method, and
balance display method.
1. What is an object in Java?
2. Which keyword is commonly used to create an object?
3. What happens when two references point to the same object?