Classes and Objects in OOP: Blueprint, Instance and Worked Example

See exactly what a class defines, what new creates, and how two references can reach one object. Follow two Java traces with object counts and final values.

KnowledgeGate Team

Exam prep & CS education

Updated 18 Nov 20265 min read
Classes and Objects in OOP: Blueprint, Instance and Worked Example

A class is often called a blueprint and an object a house, but that slogan leaves key questions unanswered. Where do values live, what does new create, and why can two variables observe the same change? Java makes the distinction concrete: allocation creates objects, references reach them, and method calls mutate the receiver's state.

Class versus object: the definition behind the blueprint analogy

A class is a programmer-declared type grouping the structure and permitted behaviour of a family of values. An object is one runtime instance, with its own identity and instance state. One class declaration can create zero, one or many objects.

The blueprint can say that every account has an owner, a balance and a deposit operation. It does not hold Asha's 1200 or Ravi's 800. Those values belong to separate objects. deposit(300) is declared once but invoked on a particular receiver.

These ideas support the foundation covered in Coding & Skills. For abstraction, encapsulation, inheritance and polymorphism, read Object Oriented Technology and Programming Languages next.

Read one Java class as data, construction and behaviour

The class definition is:

java
class BankAccount {
    String owner;
    int balance;
    static int accountCount = 0;
    static String bankCode = "KG01";

    BankAccount(String owner, int openingBalance) {
        this.owner = owner;
        this.balance = openingBalance;
        accountCount++;
    }

    void deposit(int amount) {
        balance = balance + amount;
    }

    void withdraw(int amount) {
        balance = balance - amount;
    }
}

owner and balance are per-object fields. accountCount and bankCode belong to the class and are shared. The constructor sets starting state. In this.owner = owner, the left side is the current receiver's field and the right side is the parameter.

deposit adds to the receiver's balance; withdraw subtracts from it. A production BankAccount would also validate amounts, persist data, calculate interest and coordinate concurrent changes. Those concerns do not alter how fields, methods, constructors and references work. The Java Programming course is the guided language-specific follow-up.

Worked example: create two objects, mutate them and introduce an alias

Trace these statements in order:

  1. BankAccount a = new BankAccount("Asha", 1200); creates O1 with balance 1200; accountCount goes from 0 to 1.

  2. BankAccount b = new BankAccount("Ravi", 800); creates O2 with balance 800; accountCount goes from 1 to 2. Both expose shared bankCode="KG01".

  3. a.deposit(300); makes O1 1200 + 300 = 1500. O2 stays 800.

  4. b.withdraw(250); makes O2 800 - 250 = 550. O1 stays 1500.

  5. BankAccount c = a; adds a reference, not an object. Both a and c point to O1; accountCount stays 2.

  6. c.withdraw(200); makes O1 1500 - 200 = 1300, visible through a and c.

Object

References

Owner

Final balance

Shared class value

O1

a, c

Asha

1300

KG01

O2

b

Ravi

550

KG01

The represented balance is 1300 + 550 = 1850. The count result is 2 objects, 3 non-null reference variables, and accountCount=2.

A class-and-object diagram traces BankAccount objects, with references a and c sharing O1 at balance 1300, b targeting O2 at 550, and total balance 1850.

Separate identity, state, behaviour and references

Identity says which object it is, O1 or O2. State is current field data, such as Asha and 1300. Behaviour is an operation such as withdraw(200). A reference such as a, b or c lets code reach an object.

c = a copies the reference value, not O1 or its fields. Thus a == c is true, and mutation through c is visible through a. Reassigning c = b would only redirect c to O2, without merging objects or copying balances.

If BankAccount d = new BankAccount("Asha", 1300); runs next, O3 looks like O1 but has a different identity, so a == d is false. Both the object count and accountCount become 3. Equal-looking state is not identical identity.

Instance members, class members and constructor traps

O1 holds 1300 while O2 holds 550 because balance is an instance field. Both access bankCode="KG01" and accountCount=2 because these are shared static members, not per-object copies.

In Java, new BankAccount("Asha", 1200) creates an object and runs the matching constructor. BankAccount x; only declares a reference, and BankAccount y = null; also creates no object. Since the class declares a two-argument constructor, new BankAccount() is invalid unless a no-argument constructor is added.

Languages differ in allocation, references, value semantics, cleanup and class features. Java allocates these instances with new and lets a, b and c hold references; a language with value semantics may copy state instead. Across those variations, a class defines a type and operations, while an object is one instance with identity and state.

Common misconceptions and how to repair them

Tempting claim

Correction

Trace evidence

A class is an object

A class declaration can exist with zero instances

Objects appear only when the trace executes new

c = a copies O1

It copies a reference and creates an alias

c.withdraw(200) changes the balance seen through a

Three references mean three objects

References and objects must be counted separately

a, b and c reach only O1 and O2

Changing O1's balance does not change O2's. Changing static bankCode from "KG01" to "KG02", however, makes both objects observe "KG02". accountCount is one shared counter, not a per-object serial number.

A constructor is not an ordinary method with a return type. this.owner = owner does not assign a variable to itself: the left side is the object's field and the right side is the parameter.

When counting, first ask, Did this line use new? It is a clue, not a universal rule. Factory methods and other languages may hide different creation rules, so inspect what the code creates.

How exams test classes and objects

Stable question shapes include definition matching, counting classes, objects and references, tracing state changes, and separating instance from static members. Distractors count aliases as objects, equate similar-looking objects, or treat static fields as per-instance copies.

Try this standalone trace from a reset state where accountCount=0:

java
BankAccount p = new BankAccount("Neha", 500);
BankAccount q = p;
BankAccount r = new BankAccount("Neha", 500);
q.deposit(100);

The first and third lines create 2 objects; all 3 references are non-null. p and q point to O1, whose balance becomes 500 + 100 = 600. r points to O2 at 500. Thus p == q is true, q == r is false, and accountCount=2.

A reference-and-heap diagram shows p and q sharing O1 with balance 600, r targeting O2 with balance 500, and two objects versus three references.

Classes and Objects MCQs provides 12 exam-style drills on constructors, nested classes and concept boundaries. The BankAccount trace above instead develops aliases, reference counts and heap state line by line.

The short version and next step

Keep four points clear:

  • A class defines a reusable type.

  • An object is a runtime instance with identity.

  • Instance fields give each object independent state, such as O1 at 1300 and O2 at 550.

  • Multiple references can point to one object, as a and c both point to O1.

For a five-minute task, redraw the first diagram, explain why c = a leaves accountCount at 2, and solve the Neha trace from memory. Then replace only q = p with q = new BankAccount("Neha", 500). The result is 3 objects, accountCount=3, p at 500, q at 600, and r at 500.

Use the Java course above for structured syntax and implementation practice. If you want to connect these object-oriented basics with placement-oriented coding across languages, continue with Coding for Placements.