Stop 04 · Classes & ObjectsTopics · 5/8

static Members

Part of the Java Roadmap

In short

Some things belong to the whole class, not to any single object: a counter of how many accounts exist, the GST rate, a helper like Math.max. That is what static is for.

static fields and methods belong to the class itself, not to any one object, so all objects share them. They suit counters, constants and utility methods like Math.max.

The jaadu points up at one big round clock on a classroom wall while rows of small desks each have their own notebook.

Think of it like this

The classroom wall clock

Every student in the classroom has their own notebook and pen, but there is one wall clock that everybody shares. Change the clock and everyone sees the new time. Instance fields are the notebooks; a static field is the single wall clock that belongs to the room itself.

Watch it happen

Three accounts created, one shared counter

count = 0
0
acc1
1
acc2
2
acc3
3

1/4Account.count is static. There is exactly one copy, stored with the class.

The big ideas

What you need to know

Shared by all objects

A static field has one copy for the class. Access it as Account.count, not through an object.

Utility methods

Math.sqrt, Integer.parseInt and your own helpers can be static because they do not need object data.

No this in static

A static method has no current object, so it cannot use instance fields or this directly.

Constants

public static final double GST_RATE = 0.18; one shared, unchangeable value.

See it in code

Walk through the code

java
1public class Account {2    public static final String BANK_NAME = "Bharat Bank";3    private static int count = 0;4 5    private final String accountNumber;6    private double balance;7 8    public Account(double openingBalance) {9        count++;10        this.accountNumber = "BB" + String.format("%04d", count);11        this.balance = openingBalance;12    }13 14    public static int getCount() {15        return count;16    }17 18    public static double interest(double principal, double ratePercent) {19        return principal * ratePercent / 100;20    }21 22    public String getAccountNumber() { return accountNumber; }23 24    public static void main(String[] args) {25        Account a = new Account(1000);26        Account b = new Account(2500);27        System.out.println(a.getAccountNumber() + ", " + b.getAccountNumber()); // BB0001, BB000228        System.out.println(Account.getCount());               // 229        System.out.println(Account.interest(10000, 7));       // 700.030        System.out.println(BANK_NAME);31    }32}

1/4A constant shared by every account, named in capitals.

A static constant, a static counter that numbers each account, and a static utility method.

Save yourself the pain

Common mistakes

Oops

Using balance inside a static method.

Do this instead

Static methods have no object, so they cannot see instance fields. Pass the value in, or make the method non-static.

Oops

Making fields static just so main can reach them easily.

Do this instead

Then every object shares one value, a hidden bug. Create an object in main instead.

Your mission

Ticket counter

~15 min

Quick check

Why can't a static method use this?

Go deeper

  1. 1Add a static counter that tracks how many objects of a class have been created.
  2. 2Write a static utility method and call it without creating an object.
  3. 3Try to use an instance field inside a static method and read the error.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge