Objects

Objects

A school program needs reliable data and clear rules. An object is a particular instance with its own state. Copying a reference gives another way to reach the same student, rather than creating a second student.

  1. Understand

    An object is a particular instance with its own state.

  2. Apply

    Trace the Java example, predict its output and explain the result before running it.

  3. Avoid mistakes

    Two references may point to one object. Use an explicit copying strategy when independent state is required.

Objects

In Java, everything starts with objects. Whether you're building a banking system, a video game, or an e-commerce platform, objects are the building blocks. If classes are blueprints, objects are the actual houses built from those blueprints.

What is an Object in Java?

An object is a runtime instance of a class. It is an entity that has:

  • State (fields/attributes): Describes the object (e.g., name, age, balance)
  • Behavior (methods): What the object can do (e.g., walk, talk, deposit)

How to Create an Object in Java

To create an object, we use the new keyword followed by the class constructor. Here's a simple example:

class Dog {
    String name;
    int age;

    void bark() {
        System.out.println(name + " says: Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog(); // Creating an object
        myDog.name = "Buddy";
        myDog.age = 3;
        myDog.bark(); // Calling a method on the object
    }
}
Buddy says: Woof!

Explanation

Java · example discussed here

class Dog {
    String name;
    int age;

    void bark() {
        System.out.println(name + " says: Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog(); // Creating an object
        myDog.name = "Buddy";
        myDog.age = 3;
        myDog.bark(); // Calling a method on the object
    }
}

In the above program:

  • Dog is the class – the blueprint.
  • myDog is the object – the actual dog instance.
  • We set the dog's name and age using dot notation, and then made it bark using myDog.bark().

Multiple Objects from One Class

You can create as many objects as you want from a single class. Each will have its own copy of fields and methods.

public class Main {
    public static void main(String[] args) {
        Dog dog1 = new Dog();
        dog1.name = "Max";
        dog1.age = 4;

        Dog dog2 = new Dog();
        dog2.name = "Bella";
        dog2.age = 2;

        dog1.bark();
        dog2.bark();
    }
}
Max says: Woof!
Bella says: Woof!

Understanding Memory Allocation

When you create an object using new, Java allocates memory on the heap. The variable (myDog) stores a reference (address) to that memory location.

Constructor: The Object Initializer

Instead of setting each field manually, we can initialize values using a constructor.

class Dog {
    String name;
    int age;

    Dog(String n, int a) {
        name = n;
        age = a;
    }

    void bark() {
        System.out.println(name + " barks at age " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Dog dog = new Dog("Rocky", 5);
        dog.bark();
    }
}
Rocky barks at age 5

Object Methods vs Class Methods

Methods that work on object-specific data (like bark()) are instance methods. You call them through objects. We'll cover static (class-level) methods in another tutorial.

Real-world Analogy

Think of a class like a "Car" manual, and an object like an actual car you can drive. Multiple people can build cars from the same manual, but each car has its own color, speed, and mileage.

Why Objects Matter

Java is an Object-Oriented Programming language. Understanding objects is foundational for:

Summary

To recap:

  • Objects are created from classes.
  • They hold data in fields and define behavior through methods.
  • The new keyword is used to create object instances.
  • Constructors help initialize objects conveniently.

Important boundaries and mistakes

Two references may point to one object. Use an explicit copying strategy when independent state is required.

Try the school example

Before running this Java program, write down each line you expect to see. Identify the value, branch, object or exception responsible for each line. Then explain whether the following assumption is valid: Two references may point to one object.

Java 17 · Main.java · complete practice program

public class Main  {
  interface Named  {
    String label();
  }
  static class Student implements Named  {
    static String school = "River School";
    String name;
    Student(String name)  {
      this.name = name;
    }
    public String label()  {
      return name;
    }
  }
  public static void main(String[] args)  {
    Student a = new Student("Diya");
    Student b = a;
    b.name = "Aarav";
    System.out.println(a.name);
  }
}

Now change one thing: Construct a second Student instead of assigning b = a. Predict the new behavior before checking the worked variation.

Worked answer and reasoning

Java 17 · Main.java · source repeated for this answer

public class Main  {
  interface Named  {
    String label();
  }
  static class Student implements Named  {
    static String school = "River School";
    String name;
    Student(String name)  {
      this.name = name;
    }
    public String label()  {
      return name;
    }
  }
  public static void main(String[] args)  {
    Student a = new Student("Diya");
    Student b = a;
    b.name = "Aarav";
    System.out.println(a.name);
  }
}

Expected output

Aarav

An object is a particular instance with its own state. Copying a reference gives another way to reach the same student, rather than creating a second student.

Two references may point to one object. Use an explicit copying strategy when independent state is required.

Run with javac Main.java, then java Main. The declaration inside Main keeps the helper type available to this standalone example.

Worked variation: Construct a second Student instead of assigning b = a. Changing b.name then leaves a.name unchanged.

Check your understanding

For the complete practice program in this lesson, which output is expected on Java 17?
Choose one answer. You can try again.

Answer and explanation

Aarav

An object is a particular instance with its own state. Copying a reference gives another way to reach the same student, rather than creating a second student. Two references may point to one object. Use an explicit copying strategy when independent state is required.

Check a common mistake

After Student b = a, how many Student objects did that assignment create?
Choose one answer. You can try again.

Answer and explanation

None. b receives a reference to the existing object.

Copying a reference does not invoke a constructor. Both names can reach the same object until a reference is reassigned.

What to remember

An object is a particular instance with its own state. Copying a reference gives another way to reach the same student, rather than creating a second student. Two references may point to one object. Use an explicit copying strategy when independent state is required.

Related explanations: Java Variables, Java If Statement - Syntax and Examples, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide and Java 17 language specification.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Access Modifiers →