IAE Academy

FOR CORE AND IT JOBS · CHAPTER 1 OF 7

Classes and objects in Java

This chapter explains how a Java program is built from objects that keep state and behaviour together. It shows what variables, assignment and method calls really copy, and where objects live in memory. You need it to read and write any Java class without guessing.

Updated 30 Oct 2026

What you will be able to do

You will write a Java class with fields, a constructor and methods, and build objects from it with new. You will say what a variable holds, and what an assignment or a method call copies. You will explain why a swap method cannot work in Java, and when to use this and static. You will finish with a bank account class written and used in full.

Before you start

  1. No earlier chapter: this is the first.

  2. Variables, assignment, and calling a function with arguments, in any language.

  3. Reading simple Java: int, double, boolean, String, if, return, +=, -= and ++, // comments, and System.out.println.

  4. Joining text with + in Java, and running a class that has a main method.

  5. Keeping each public class in its own file named after it, with the files in one folder.

Objects and classes

One variable for a balance and ten functions that change it works until you need a thousand accounts. An object keeps the data and the code allowed to change it together. The data is its state. The methods are its behaviour. A class is the written template. Each time new runs on a class, one object is built with its own copy of every field declared without the word static; these are its instance fields. A constructor has the class's name and no return type; new runs it once to set the first state.

AN OBJECT

Holds for every Java object. State is the values of the object's fields at this moment. Behaviour is the set of methods that read or change them. Two objects of one class run the same methods, but each has its own copy of every instance field.

Here is the whole class, in Java 17, with class names in PascalCase and fields and methods in camelCase. The word private means only code inside the class may touch that field; public means any class may use it. The word this is explained below.

public class BankAccount {
    private String owner;
    private double balance;

    public BankAccount(String owner, double opening) {
        this.owner = owner;
        this.balance = opening;
    }
    public void deposit(double amount) { balance += amount; }
    public boolean withdraw(double amount) {
        if (amount > balance) return false;
        balance -= amount;
        return true;
    }
    public double getBalance() { return balance; }
}

WATCH OUT FOR

Mixing up class and object. Test: write new BankAccount(...) twice. You get two objects with separate balances, from one class written once.

WORKED EXAMPLE

Run these lines inside a main method, with the class above. What do they print?

BankAccount a = new BankAccount("Asha", 1000);
a.deposit(500);
boolean ok = a.withdraw(2000);
a.withdraw(200);
System.out.println(ok + " " + a.getBalance());
Solution
  1. new builds one object and runs the constructor. The int 1000 is converted to the double parameter, so balance starts at 1000.0, and the variable a refers to the object.

  2. deposit(500) runs on the object a refers to: 1000.0 + 500 = 1500.0.

  3. withdraw(2000): the test 2000 > 1500.0 is true, so the method returns false before touching balance. The state is unchanged and ok is false.

  4. withdraw(200): 200 > 1500.0 is false, so the balance becomes 1500.0 - 200 = 1300.0. Its return value, true, is not stored, which Java allows.

  5. Joining a boolean, a String and a double with + builds one String. Java writes this double as 1300.0, with its decimal point.

Answer: false 1300.0

References, the heap and the stack

A variable of a primitive type, such as int, double or boolean, holds the value itself. A variable of a class type holds a reference: a value that locates one object. Below, ref(o) stands for a reference to an object o. Objects live on the heap, memory shared by the whole program. Each running method call gets a stack frame for its local variables and parameters, which vanishes when the method returns. An object stays while the program can still reach it through some reference; after that, the garbage collector may reclaim its memory.

WHAT A VARIABLE HOLDS

Holds for every Java variable, since its type is either primitive or a reference type such as a class. x is an int and holds the number 5. a is a BankAccount and holds a reference to an object o on the heap, not the object itself. A reference may also be null, which refers to no object.

Stack

Heap

Holds

local variables and parameters of each call

objects made by new, with their fields

Created

when a method is called

each time new runs

Removed

when the method returns

after nothing can reach it, when the garbage collector reclaims it

ASSIGNING A REFERENCE

Holds when a and b are variables of one class type. The reference is copied, the object is not: no new object is built, because new did not run. After this line a and b are two names for the one object o.

SHARING ONE OBJECT

Holds while a and b, of a class type, still refer to the same object. A change here means a change to the object's fields, for example through deposit. Reassigning either variable to another object ends the sharing.

PASSING AN ARGUMENT

Holds for every Java call. p is the parameter, a new variable in the called method's frame. For an int the copied value is the number; for a class type it is the reference. So Java passes references by value: the method can reach the caller's object but not the caller's variable.

WATCH OUT FOR

The claim that Java passes objects by reference. Test: inside a method, assign a new object to a BankAccount parameter. If the caller's variable still refers to its old object, only a copy of the reference was passed.

WORKED EXAMPLE

BankAccount is the class above, saved in its own file in the same folder as Demo. The word static on swap, swapAccounts and bonus lets main call them without building a Demo object; the last section explains static. What does this print?

public class Demo {
    static void swap(int p, int q) { int t = p; p = q; q = t; }
    static void swapAccounts(BankAccount p, BankAccount q) {
        BankAccount t = p; p = q; q = t;
    }
    static void bonus(BankAccount p) { p.deposit(50); }
    public static void main(String[] args) {
        int x = 3, y = 7;
        swap(x, y);
        System.out.println(x + " " + y);
        BankAccount a = new BankAccount("A", 100);
        BankAccount b = new BankAccount("B", 200);
        swapAccounts(a, b);
        System.out.println(a.getBalance());
        bonus(a);
        System.out.println(a.getBalance());
    }
}
Solution
  1. swap(x, y): p receives a copy of 3 and q a copy of 7 in swap's own frame. The three assignments exchange the copies only. When swap returns, its frame is gone and x and y still hold 3 and 7.

  2. swapAccounts(a, b): p and q receive copies of the two references. Exchanging them changes only the local variables p and q. The caller's a still refers to the account with balance 100.0.

  3. bonus(a): p receives a copy of the reference in a, so p and a refer to one object. p.deposit(50) changes that object: 100.0 + 50 = 150.0.

  4. Back in main, a refers to that same object, so it now shows 150.0.

Answer: Three lines: 3 7, then 100.0, then 150.0

this and static

Each object has its own copy of every instance field. A static field has exactly one copy, owned by the class, and exists even when no object does. Inside an instance method, this is the reference to the object the method was called on; inside a constructor, it is the object being built. That is how deposit knows whose balance to change.

FIELD SLOTS IN MEMORY

Holds for one class with no inherited fields, counting only objects built from this class itself. S is the number of field slots, n the number of those objects that exist, k the instance fields per object, s the static fields of the class (one copy each, however many objects). All four are plain counts.

THE MEANING OF THIS

Holds for every call of an instance method, where a holds ref(o). A bare field name such as balance inside deposit means this.balance, unless a parameter or local variable has the same name; then you write this.balance, as in the constructor above. A static method belongs to the class and has no this, even when called through an object.

Add a counter of accounts built. Put this field, constructor and method in the class, replacing the old constructor:

private static int count = 0;
public BankAccount(String owner, double opening) {
    this.owner = owner;
    this.balance = opening;
    count++;
}
public static int getCount() { return count; }

Then, in a main method that builds no other account:

new BankAccount("A", 100);
new BankAccount("B", 200);
System.out.println(BankAccount.getCount()); // prints 2

The count is asked of the class, so no account variable is needed.

WATCH OUT FOR

A static method that uses the bare name balance. Test: ask which object's balance. A static method has no this, so the compiler rejects it.

Check yourself

CHECK YOURSELF 1

With the BankAccount class of this chapter:

BankAccount a = new BankAccount("A", 100);
BankAccount b = a;
b.deposit(50);
System.out.println(a.getBalance());

What is printed?

  1. (A)

    100.0

  2. (B)

    150.0

  3. (C)

    50.0

  4. (D)

    The code does not compile

Answer and solution

Answer: (B)

  1. b = a copies the reference, so a and b refer to one object.

  2. b.deposit(50) changes that object: 100.0 + 50 = 150.0.

  3. Reading through a shows the same object, so 150.0 is printed.

CHECK YOURSELF 2

After this code runs, how many BankAccount objects exist on the heap? Count only BankAccount objects.

BankAccount a = new BankAccount("A", 1);
BankAccount b = new BankAccount("B", 2);
BankAccount c = a;
BankAccount d = null;
BankAccount e = new BankAccount("E", 3);
Answer and solution

Answer:

  1. Objects are built only when new runs.

  2. new runs three times: for a, for b and for e.

  3. c = a copies a reference and d = null refers to nothing, so neither builds an object.

  4. All three objects are still referred to, so the count is 3.

CHECK YOURSELF 3One or more options are correct

With the BankAccount class of this chapter, which statements about a Java method call are true?

  1. (A)

    A parameter receives a copy of the value held by the argument.

  2. (B)

    If the argument refers to a BankAccount object, the method can change that object's balance by calling deposit on the parameter.

  3. (C)

    Assigning a new object to the parameter inside the method changes the caller's variable.

  4. (D)

    Changing an int parameter inside the method changes the caller's int variable.

Answer and solution

Answer: (A), (B)

  1. A is the passing rule: every call copies the value.

  2. B is true: the copied reference refers to the same object, so deposit changes that object's balance.

  3. C is false: only the local copy of the reference is reassigned.

  4. D is false: the method changes its own copy of the number.

CHECK YOURSELF 4

A class with no inherited fields has 3 instance fields and 2 static fields. A program has built 4 objects of it, and all 4 still exist. How many field slots exist in total?

Answer and solution

Answer:

  1. Use S = n k + s with n = 4, k = 3, s = 2.

  2. Instance slots: 4 x 3 = 12.

  3. Static fields have one copy each: 2.

  4. S = 12 + 2 = 14.

Used in these GATE topics