๐ฏ What you will learn
Arc A gave you primitives, control flow, and methods โ all living inside static methods in a single class. Arc B introduces Object-Oriented Programming: modelling real-world entities as classes with their own data (fields) and behaviour (methods). By the end of this lesson you will understand:
- What a class is and how to define fields and methods inside it
- How
newallocates an object on the heap and returns a reference - Why multiple objects created from the same class have independent state
- The difference between
static(class-level) and instance members - Default field values and the reference vs value distinction
๐งฑ What is a class?
A class is a blueprint or template that defines the structure and behaviour of objects. It specifies what data each object holds (fields) and what operations it can perform (methods). A object is a concrete instance of that blueprint โ created at runtime using the new keyword.
When you write SBAccount acc = new SBAccount(), the JVM allocates memory on the heap for a new SBAccount object, sets all fields to their defaults, and returns a reference. The variable acc lives on the stack and holds that reference โ not the object itself.
SouthBank India processes 2 million savings accounts across 400 branches. Arjun, a backend engineer in Mumbai, is building the core account engine. His first task: model a BankAccount as a Java class โ with fields for owner, number, and balance, and methods for deposit, withdraw, and summary. Then he'll track bank-wide stats using static fields.
Key rule: Fields without explicit initialisation get zero-values: numeric types โ 0 / 0.0, boolean โ false, object references โ null. Local variables inside methods are NOT automatically initialised โ the compiler forces you to assign before reading.
๐ฆ Multiple objects โ independent state
Each call to new produces a fresh, independent object on the heap. Modifying one object's fields never affects another object's fields, even though both were created from the same class blueprint.
โก Static vs instance members
An instance field exists once per object โ every Ledger has its own balance. A static field exists once per class โ all instances share the same copy. The same distinction applies to methods: instance methods have access to this (the current object); static methods do not.
๐ Quick reference โ class vs object terminology
| Term | Meaning | Example |
|---|---|---|
| Class | Blueprint / type | class SBAccount { } |
| Object | Instance created from class | new SBAccount() |
| Field | Variable belonging to object | double balance |
| Method | Function belonging to object | void deposit(...) |
| Constructor | Special method that initialises object | covered in lesson 09 |
| static field | One copy shared across all instances | static int totalAccounts |
| static method | Called on the class, not an object | StaticVsInstance.summary() |
๐ค Interview Q&A
A class is a compile-time blueprint/template; an object is a runtime instance created from that blueprint via new. You can create many objects (instances) from one class. The class defines structure and behaviour; the object holds actual data.
double field?0.0. Java guarantees all instance fields are zero-initialized: numeric fields to 0, boolean to false, object references to null. Local variables (inside methods) are NOT initialized and must be explicitly set before use.
An instance field has one copy per object โ each Ledger has its own balance. A static field has one copy per class โ StaticVsInstance.totalAccounts is shared by every instance. Changing a static field via one object affects what all objects see.
Ledger a = l1; a.balance = 0 also changes l1.balance.In Java, object variables hold references (memory addresses), not objects themselves. Assigning a = l1 copies the reference, so a and l1 point to the same heap object. Mutating a.balance mutates the single shared object. To get an independent copy you'd need to explicitly copy field by field or implement a clone.
A static method belongs to the class, not to any instance. When you call StaticVsInstance.summary() there is no this โ no particular object is associated with the call. Therefore this.balance would be meaningless. Instance fields require an object (this) to determine which object's copy to read. Static methods can only access other static members or work with objects passed as parameters.
๐ Quiz โ 5 questions
static field is:SBAccount acc; โ what is acc's value before new?l1.credit(5000). Does l2.balance change?summary()?Next: Constructors and the this keyword โ
โ ๏ธ Common mistakes
SBAccount.deposit(100) fails โ you must call on an object: acc.deposit(100). Instance methods require a receiver object; calling them on the class name is a compile error.acc.summary() compiles but misleads readers โ it hides that summary is static. Prefer StaticVsInstance.summary() to make the static nature explicit and clear.SBAccount acc = SBAccount() doesn't compile โ new is mandatory. Without it you're calling a static method that doesn't exist. Always write new SBAccount() to allocate an object.Ledger a = l1; a.balance = 0 mutates l1's balance too. Assignment copies the reference, not the object. If you need an independent copy, clone or copy field by field explicitly.โ Key takeaways
- A class is a blueprint;
newcreates an object (instance) on the heap. - Each object owns its own copy of instance fields โ mutating one doesn't affect another.
staticfields and methods belong to the class, not to any instance.- Object variables hold references. Assigning
b = amakes both point to the same object. - Uninitialized instance fields default to 0 / false / null; local variables must be explicitly assigned.
From classes to production Spring Boot
Join the Best AI Programs โ three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses โ