Object-Oriented Programming Basics

Fields, Methods & State

15 min Lesson 2 of 14

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.

public class BankAccount { // instance fields — each object gets its own copies String ownerName; double balance; String accountNumber; }

To see those separate copies in action, create two objects and change one:

public class Main { public static void main(String[] args) { BankAccount alice = new BankAccount(); alice.ownerName = "Alice"; alice.balance = 500.0; BankAccount bob = new BankAccount(); bob.ownerName = "Bob"; bob.balance = 1200.0; // changing Bob's balance does NOT touch Alice's bob.balance = 900.0; System.out.println(alice.ownerName + ": " + alice.balance); // Alice: 500.0 System.out.println(bob.ownerName + ": " + bob.balance); // Bob: 900.0 } }
Key idea — each object is independent. 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).

public class BankAccount { String ownerName; double balance; // instance method — reads and modifies the calling object's state public void deposit(double amount) { balance = balance + amount; // 'balance' here means THIS object's balance } public void withdraw(double amount) { if (amount <= balance) { balance = balance - amount; } else { System.out.println("Insufficient funds."); } } public void printSummary() { System.out.println(ownerName + " — Balance: $" + balance); } }

Now the class has both data and the operations that belong to that data:

public class Main { public static void main(String[] args) { BankAccount alice = new BankAccount(); alice.ownerName = "Alice"; alice.balance = 0.0; alice.deposit(500.0); alice.deposit(200.0); alice.withdraw(100.0); alice.printSummary(); // Alice — Balance: $600.0 } }

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.

BankAccount alice = new BankAccount(); alice.ownerName = "Alice"; alice.balance = 0.0; BankAccount bob = new BankAccount(); bob.ownerName = "Bob"; bob.balance = 0.0; alice.deposit(300.0); // only alice.balance changes bob.deposit(800.0); // only bob.balance changes alice.printSummary(); // Alice — Balance: $300.0 bob.printSummary(); // Bob — Balance: $800.0
Return values from methods. Methods can return data too. Use the appropriate return type instead of void:
public double getBalance() { return balance; }
Calling 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.
public class Counter { int count; // instance field — persists between calls public void increment() { int step = 1; // local variable — lives only inside this method count = count + step; } }
Common pitfall: forgetting to initialise fields. In Java, instance fields of numeric types default to 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.