Course contentsShow
← Java
Lesson 361 of 2,8709. Static Members and Utility ClassesFree lesson

static Fields and Memory: Single Copy

How static fields are stored once in memory and shared, reducing memory overhead for shared data.

static Fields and Memory: Single Copy

What You'll Learn

You'll understand how static fields use memory efficiently by existing in only one place, shared by all instances of a class.

The Memory Advantage of static

When you declare a field as static, Java stores exactly one copy of that field in memory, regardless of how many objects you create. Non-static (instance) fields, by contrast, get their own separate copy in every single object.

Visualizing the Difference

Imagine a company where every employee has their own desk (instance field) but shares a single coffee machine (static field). If you have 1,000 employees:

  • Instance fields: 1,000 desks taking up space
  • Static field: 1 coffee machine shared by all

In Java, this means if you create 10,000 objects of a class with a static field, that field occupies memory once, not 10,000 times.

How Java Manages This in Memory

public class Config {
    private static String appName = "MyApp";  // One copy, stored at class level
    private int userId;                        // Each object gets its own copy
}

Config c1 = new Config();
Config c2 = new Config();
Config c3 = new Config();

In memory:

  • One appName field exists (class-level storage)
  • Three userId fields exist (one per object)

When This Matters

Use static for data that truly belongs to all instances: configuration constants, counters tracking total objects created, or shared resources. You reduce memory overhead and make your intent clear—this data isn't about individual objects, it's about the class as a whole.

Key Takeaway: Static fields are stored once in class-level memory and shared by all instances, eliminating redundant copies and reducing memory consumption for shared data.