๐ŸŽฏ 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 new allocates 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
๐Ÿ’ผ Interview reality: "What is the difference between a class and an object?" is asked in virtually every Java entry-level round. The answer must include blueprint vs instance, heap allocation, and independent state. This lesson makes that muscle memory.

๐Ÿงฑ 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.

๐Ÿฆ
Real-world scenario ยท SouthBank India
SouthBank India โ€” Arjun's account engine (Mumbai)

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.

javac AccountBasics.java && java AccountBasics
AccountBasics.javaLesson 08 ยท class fields and methods
stdout
SouthBank India โ€” SB-9001 (Arjun) Opening balance : โ‚น50,000.00 Deposit โ‚น 15,000.00 โ†’ balance: โ‚น65,000.00 Withdraw โ‚น 8,000.00 โ†’ balance: โ‚น57,000.00 DECLINED โ‚น100,000.00 (insufficient funds) Closing balance : โ‚น57,000.00

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.

javac ObjectState.java && java ObjectState
ObjectState.javaLesson 08 ยท multiple objects with independent state
stdout
Account Balance (โ‚น) Txns ---------------------------------- SB-1001 31,000.00 3 SB-1002 100,000.00 1 SB-1003 12,000.00 0 Each Ledger object holds its OWN state.

โšก 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.

javac StaticVsInstance.java && java StaticVsInstance
StaticVsInstance.javaLesson 08 ยท static fields and methods
stdout
=== SouthBank India โ€” Bank-Wide Stats === Accounts opened : 3 Total deposited : โ‚น205,000.00

๐Ÿ“‹ Quick reference โ€” class vs object terminology

TermMeaningExample
ClassBlueprint / typeclass SBAccount { }
ObjectInstance created from classnew SBAccount()
FieldVariable belonging to objectdouble balance
MethodFunction belonging to objectvoid deposit(...)
ConstructorSpecial method that initialises objectcovered in lesson 09
static fieldOne copy shared across all instancesstatic int totalAccounts
static methodCalled on the class, not an objectStaticVsInstance.summary()

๐ŸŽค Interview Q&A

EasyWhat is the difference between a class and an object in Java?
+

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.

EasyWhat is the default value of an uninitialized 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.

MediumWhat is the difference between a static field and an instance field?
+

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.

MediumExplain why 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.

HardWhy can a static method NOT access instance fields?
+

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

Question 1 of 5
What keyword creates a new object on the heap?
Question 2 of 5
A static field is:
Question 3 of 5
SBAccount acc; โ€” what is acc's value before new?
Question 4 of 5
You call l1.credit(5000). Does l2.balance change?
Question 5 of 5
Which is a valid call for a static method summary()?
0
/ 5
Quiz complete!

Next: Constructors and the this keyword โ†’

โš ๏ธ Common mistakes

Calling instance method on the class name
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.
Calling static method on an instance
acc.summary() compiles but misleads readers โ€” it hides that summary is static. Prefer StaticVsInstance.summary() to make the static nature explicit and clear.
Forgetting new โ€” using the class name as a value
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.
Two variables pointing to the same 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; new creates an object (instance) on the heap.
  • Each object owns its own copy of instance fields โ€” mutating one doesn't affect another.
  • static fields and methods belong to the class, not to any instance.
  • Object variables hold references. Assigning b = a makes both point to the same object.
  • Uninitialized instance fields default to 0 / false / null; local variables must be explicitly assigned.
Africa & India's Best AI Academy
๐Ÿค– AI-first ๐ŸŽ“ Placement Assistance

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 โ†’