Back to Keywords: static
Q1Question 1 of 4

What is the static keyword and why is main static?

Key Concepts
  • static means "belongs to the class, not to any object" – one shared copy for all instances
  • static field: one copy shared by all objects (e.g., a bank-wide interest rate or a counter)
  • static method: called on the class, not on an object – cannot access instance fields or this
  • static block: runs once when the class is loaded, used for one-time initialization
  • static nested class: a nested class that does not need an instance of the outer class
  • main is static because the JVM calls it before any object exists

Definition

static means the member belongs to the class itself, not to any particular object. There is one shared copy, accessed via the class name.

static can be applied to four things:

  • static field: One copy shared by all instances. Stored in Metaspace with the class, not on the heap with each object.
  • static method: Called on the class (Account.getCount()) without creating an object. Cannot use this or access instance fields.
  • static initializer block: Code that runs once when the class is first loaded. Used for complex one-time setup.
  • static nested class: A class defined inside another class that does not require an outer instance.

A bank's base interest rate is static: there is one rate for the whole bank, not a separate copy per customer. Any branch can read it via the head office (class name), and changing it affects everyone. A customer's balance is instance: each customer has their own copy.

Understanding the Concepts

What is a static field?

A static field has one copy shared by all instances of the class. It is stored with the class metadata, not inside each object.

JAVA
public class Account {
    private static int accountCount = 0;   // one copy for the whole class
    private final String accountId;         // one copy per object

    public Account(String accountId) {
        this.accountId = accountId;
        accountCount++;  // shared counter incremented for every new account
    }

    public static int getAccountCount() {
        return accountCount;
    }
}

Account a = new Account("SAV2001");
Account b = new Account("CUR3001");
System.out.println(Account.getAccountCount());  // 2 (shared across all instances)

What is a static method?

A static method belongs to the class. It does not have access to this or any instance fields.

JAVA
public class InterestCalculator {
    private static final double BASE_RATE = 4.0;  // static field

    // static method: no object needed to call it
    public static double calculateInterest(double principal) {
        return principal * BASE_RATE / 100;
    }

    // Instance method: needs an object
    public void logCalculation(String accountId) {
        // can access 'this' and instance fields
    }
}

// Call static method on the class – no object created
double interest = InterestCalculator.calculateInterest(500000);

Why is main static?

When the JVM starts your program, no objects exist yet. The JVM needs to call the entry point without creating an instance. Since static methods do not require an object, main must be static.

JAVA
public class BankApp {
    public static void main(String[] args) {
        // This is the first code that runs
        // No BankApp object exists yet
        System.out.println("Starting bank application...");
    }
}

What is a static initializer block?

A static block runs once when the class is first loaded by the JVM.

JAVA
public class Config {
    private static final Map<String, String> SETTINGS;

    static {
        // Runs once when Config is first used
        SETTINGS = new HashMap<>();
        SETTINGS.put("bank.name", "National Bank of India");
        SETTINGS.put("max.transfer", "1000000");
        System.out.println("Config loaded");
    }

    public static String get(String key) {
        return SETTINGS.get(key);
    }
}

// First access triggers the static block
String name = Config.get("bank.name");  // "Config loaded" printed here
String name2 = Config.get("bank.name"); // NOT printed again – runs only once

What is a static nested class?

JAVA
public class Account {
    private BigDecimal balance;

    // Static nested class: does NOT need an Account instance
    public static class Builder {
        private String id;
        private BigDecimal balance = BigDecimal.ZERO;

        public Builder id(String id) { this.id = id; return this; }
        public Builder balance(BigDecimal b) { this.balance = b; return this; }
        public Account build() { return new Account(id, balance); }
    }
}

// Usage: no Account instance needed to create the Builder
Account account = new Account.Builder()
    .id("SAV2001")
    .balance(new BigDecimal("1000000"))
    .build();

What can a static method NOT do?

JAVA
public class Account {
    private String accountId;          // instance field
    private static int count = 0;      // static field

    public static void showCount() {
        System.out.println(count);     // OK: static accessing static
        // System.out.println(accountId);  // COMPILE ERROR: cannot access instance field
        // System.out.println(this);       // COMPILE ERROR: no 'this' in static
    }

    public void showId() {
        System.out.println(accountId); // OK: instance accessing instance
        System.out.println(count);     // OK: instance CAN access static
    }
}

Rule: static can access static. Instance can access both. Static cannot access instance.

What are common mistakes?

  • Accessing instance fields from static methods. this.field does not exist in static context. Create an object first or make the field static.
  • Calling static methods on instances. account.getAccountCount() compiles but is misleading. Call Account.getAccountCount() instead.
  • Mutable static fields without synchronization. A static counter incremented by multiple threads causes a race condition. Use AtomicInteger or synchronized.
  • Overusing static. Not everything should be static. Business logic that depends on object state should be instance methods.

What should a senior engineer know?

  • Static fields are class-level singletons. One copy per classloader. In environments with multiple classloaders (app servers, OSGi), there can be multiple copies.
  • Static methods cannot be overridden. They can be hidden by a subclass method with the same signature, but there is no polymorphic dispatch.
  • Static import lets you use static members without the class name: import static java.lang.Math.sqrt; then sqrt(25) instead of Math.sqrt(25).
  • Utility classes (like Math, Collections) are typically final classes with a private constructor and only static methods.

Visual Overview

DIAGRAM
  static = belongs to the CLASS, not to any object

  static field:    one copy, shared by all instances, in Metaspace
  static method:   called on the class, no 'this', no instance fields
  static block:    runs once when class is loaded
  static nested:   inner class that needs no outer instance

  Access rules:
    static  → can access static only
    instance → can access both static and instance

  Why main is static:
    JVM starts → no objects exist → needs static entry point

Key Takeaway

The static keyword means "belongs to the class, not to any object." A static field has one shared copy for all instances. A static method is called on the class without creating an object. A static initializer block runs once when the class is loaded. And a static nested class does not need an instance of its enclosing class.

The main method is static because the JVM must call it before any object exists. Static methods cannot access this or instance fields – they can only work with other static members or with objects passed to them as parameters.

In banking, static is used for shared constants (base interest rate), counters (account count), utility methods (interest calculation), and configuration initialization. But mutable static fields shared across threads need synchronization.

One sentence to remember: static means one copy for the class, no object needed – static can access static, instance can access both, but static cannot access instance.

---

Share
Use ← → arrow keys to navigate