Fields, Methods & State
Fields, Methods & State
In the previous lesson you learned that a class is a blueprint and an object is a real thing built from that blueprint. Now it is time to look inside the blueprint and understand its two most important ingredients: fields (what an object knows) and methods (what an object can do). Together, fields and methods give every object its own state and behaviour.
What is State?
State is simply the set of data values an object holds at any given moment. Think of a bank account: it always has a balance, an owner name, and an account number. Those are its fields, and their current values form its state. Two accounts are separate objects with separate states — changing one never touches the other.
Instance Fields
A field declared directly inside a class (but outside any method) is called an instance field. Each object gets its own private copy of every instance field.
To see those separate copies in action, create two objects and change one:
alice and bob are two separate regions of memory. The field balance exists in both regions, but they are entirely different variables. This is the whole point of object-oriented programming.
Instance Methods
A method is a named block of code that belongs to the class and operates on one specific object. When you call a method on an object, Java passes the object itself to the method invisibly — this is the implicit object reference (you will meet the explicit form, this, in the next lesson; for now just know it is there).
Now the class has both data and the operations that belong to that data:
The Implicit Object Reference
When you write alice.deposit(500.0), Java does two things: it looks up the deposit method in the BankAccount class, and it silently passes alice as the target object. Inside deposit, every bare field name like balance is shorthand for this object's balance. That is why calling the same method on two different objects produces two different results — the method code is shared, but it runs against different state each time.
void:
double amount = alice.getBalance(); retrieves the value without printing it, which is far more useful for calculations.
State Changes Over Time
The combination of fields and methods means that an object's state evolves as you call methods on it. This is stateful behaviour — and it is what separates objects from simple data structures. Imagine calling deposit ten times: balance grows ten times, yet the account object stays the same object in memory with the same reference.
Fields vs. Local Variables
Beginners often confuse instance fields with local variables. Here are the key differences:
- Instance fields are declared inside the class but outside all methods. They live as long as the object lives and hold its state between method calls.
- Local variables are declared inside a method. They exist only for the duration of that method call and are gone once the method returns.
0, booleans to false, and objects to null. Relying on those defaults silently can cause NullPointerException later. Make your initial values explicit wherever possible.
Summary
Instance fields store the state of an object — each object gets its own independent copy. Instance methods act on that state; when called, they receive an implicit reference to the specific object they are operating on. Fields and methods together transform a class from a passive data bag into an active, self-contained entity. In the next lesson you will learn how constructors let you set up that initial state at the moment an object is created.