Lesson 33 of 50 – Java Objects
66%

Java Objects

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.

1. What Is an Object in Java?

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:

  • State: name, age, course
  • Behavior: display details, study
  • Identity: a particular student object
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();
    }
}
Output:
Rahul
20
A class is a blueprint, while an object is an instance created from that class.
2. Syntax for Creating an Object

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.

3. Creating and Using an Object
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();
    }
}
Output:
Toyota
White

The object is created first, its fields are assigned values, and then its method is called.

4. Object State and Behavior

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.

5. Creating Multiple Objects

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);
    }
}
Output:
Rahul 20
Priya 21

Both objects come from the same class, but their instance field values are independent.

6. Accessing Object Members with the Dot Operator

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();
    }
}
Output:
Amit
30000
Amit
30000

The expressions e1.name and e1.showDetails() access the object's field and method.

7. Objects and Constructors

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();
    }
}
Output:
Rahul 20
Priya 21

Each constructor call initializes a separate object with the supplied values.

8. Object References

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);
    }
}
Output:
Amit
Amit
Important: s2 = s1 does not create another object. Both references point to the same object.
9. Comparing Object References

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);
    }
}
Output:
false
true

d1 and d2 refer to different objects. d1 and d3 refer to the same object.

10. Default Values of Object Fields

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);
    }
}
Output:
0
false
null

Local variables declared inside methods do not automatically receive these default values; they must be initialized before use.

11. The null Reference

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();
    }
}
Output:
Hello Student
Always make sure a reference points to a valid object before accessing its instance members.
12. Real-Life Example: Book Objects
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();
    }
}
Output:
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.

13. Practice Assignments
  1. Create a Student class and create three student objects with different names.
  2. Create an Employee class with name and salary fields. Display details for two employees.
  3. Create a Car class with brand, color, and a method to display its details.
  4. Create a Book class with a parameterized constructor.
  5. Create two reference variables that refer to the same object and demonstrate how changing a field through one reference affects the other.
  6. Create a Rectangle class with length and width fields and a method to calculate area.
  7. Create a BankAccount class with an account holder, balance, deposit method, and balance display method.
Practice Tip: Write every program yourself and observe how different objects store their own instance data.

🧠 Quick Quiz

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?