Classes & Objects
Everything in OOP starts here: a class is the blueprint, an object is the thing built from it. Interviewers use this "basic" topic to check that your mental model — instances, state, constructors, static vs instance — is actually solid.
The core idea
A class is a blueprint (a template/type) that defines what data (fields/attributes) an object holds and what it can do (methods/behavior). An object is a concrete instance of a class, created at runtime, with its own copy of the field values (its state). One class, many objects: the Car class defines wheels and drive(); each actual car is a separate object with its own color and speed.
Beginner — creating and using objects
You create an object by instantiating the class (often with the "new" keyword), which invokes a constructor — a special method that initializes the object's fields. Each object has its own state (field values) and shares the class's behavior (methods). A reference variable points to the object in memory.
Intermediate — instance vs static
- Instance members belong to each object (each car's own speed).
- Static (class) members belong to the class itself, shared by all objects (e.g., a count of cars created, or a utility method). Static methods can't access instance fields directly because they aren't tied to any object.
Objects also often need lifecycle handling: constructors to set up, and (in some languages) destructors/finalizers or explicit close() to clean up resources.
Advanced — identity, equality, and the heap
Two objects can be equal in value yet have different identity (different memory references). Languages distinguish reference equality (same object) from value equality (same contents), which you customize via equals()/hashCode() or operator overloads. Objects typically live on the heap, with references on the stack; unreferenced objects are reclaimed by garbage collection (or manual deletion). A class may also define encapsulated private state and expose a clean public interface — tying classes back to the other OOP pillars.
Worked example
Define a class Car with fields color and speed and a method accelerate(). Create two objects: myCar = new Car("red") and yourCar = new Car("blue"). Calling myCar.accelerate() changes only myCar's speed — the objects have independent state. A static field totalCars, incremented in the constructor, is shared: after two instantiations it reads 2 for the whole class, not per object.
When to use
Classes and objects are the fundamental unit of modeling in OOP: represent each real-world or domain concept (User, Order, Invoice) as a class, and create objects to represent specific instances. Use static members for data/behavior that belongs to the type as a whole rather than any instance.
Tricks and mnemonic
Remember "class = cookie cutter, object = cookie." One cutter (class) stamps out many cookies (objects), each with its own toppings (state) but the same shape (behavior).
Don't confuse the class with its objects, or instance members with static ones. The class is the definition (loaded once); each object is a separate instance with its own state. A static member is shared by all instances — changing it affects every object's view of it.
- ✓- Class = blueprint (fields + methods); object = an instance with its own state.
- ✓- Instantiation (new) invokes a constructor that initializes the object.
- ✓- Each object has independent state but shares the class's behavior.
- ✓- Instance members belong to objects; static members belong to the class (shared).
- ✓- Objects have identity (reference) vs value equality; they live on the heap.
- ✓A class is a blueprint defining fields and methods; an object is a runtime instance with its own state, created by a constructor. Objects share behavior but hold independent state, differ between instance and static members, and are distinguished by identity versus value equality — the atoms of OOP modeling.
Classes & Objects — Revision Notes
Fast revision of classes and objects.
- Class = a blueprint/type defining fields (data) and methods (behavior).
- Object = a concrete instance of a class with its own state (field values); one class → many objects.
- Instantiation (often "new") invokes a constructor that initializes the object's fields.
- Each object has independent state but shares the class's behavior; a reference points to it.
- Instance members belong to each object; static/class members belong to the class and are shared by all objects.
- Identity (same reference) vs value equality (same contents) — customized via equals()/hashCode().
- Objects usually live on the heap; unreferenced ones are reclaimed by garbage collection.
Classes & Objects — Flashcards
Cover the answer, recall, then check. 8 cards.
Q1. What is a class?
A1. A blueprint/type defining the fields (data) and methods (behavior) an object will have.
Q2. What is an object?
A2. A concrete instance of a class with its own state (field values), created at runtime.
Q3. What does a constructor do?
A3. Initializes a new object's fields when it is instantiated.
Q4. Do two objects of the same class share their field values?
A4. No — each object has its own independent state, but they share the class's methods.
Q5. Instance member vs static member?
A5. Instance members belong to each object; static (class) members belong to the class and are shared by all instances.
Q6. Why can't a static method access instance fields directly?
A6. It isn't tied to any particular object, so there's no instance whose fields to read.
Q7. Identity vs value equality?
A7. Identity = same object/reference; value equality = same contents (customized via equals()/hashCode()).
Q8. Where do objects typically live in memory?
A8. On the heap, with references on the stack; unreferenced objects are garbage-collected.
Classes & Objects — Worked Example
Worked Example
Problem: What does this print, and what is the difference between the two fields?
class Counter {
static int total = 0; // class-level (shared)
int id; // instance-level (per object)
Counter() { total++; id = total; } // constructor
}
Counter a = new Counter();
Counter b = new Counter();
print(a.id, b.id, Counter.total);
Solution: A class is a blueprint; each new creates an object with its own copy of instance fields, while static fields are shared across all objects.
Trace:
new Counter()for a: total becomes 1, a.id = 1.new Counter()for b: total becomes 2, b.id = 2.
So a.id = 1, b.id = 2, and Counter.total = 2 (one shared counter incremented twice).
Output: 1 2 2.
Each object has its own id; the single total belongs to the class and counts how many objects exist.
Answer: It prints 1 2 2.
- ✓- A class is a template; each object is an instance with its own instance fields.
- ✓-
staticmembers are shared by the whole class, not per object. - ✓- The constructor initialises a new object's state when
newis called.