Introduction
In our last lesson, we established the principle of encapsulation, focusing on how public and private modifiers control access to an object's state. All the fields and methods we discussed, like the balance in a BankAccount, were instance members—meaning each individual object gets its own separate copy.
Today, we'll explore the other side of the coin: class-level members. This lesson addresses how to use the static modifier to create fields and methods that are shared across all instances of a class. Understanding this distinction is crucial, as it allows you to manage state and behavior that belongs to the class as a whole, rather than to any single object. This concept is fundamental to many design patterns, such as the Singleton, which we will study later.
Instance Members vs. Class Members
Let's begin by solidifying the difference. In our previous lesson, every BankAccount object had its own unique balance. If you created three BankAccount objects, you created three separate balance variables in memory. These are instance variables.
But what if you need a piece of data that is common to all BankAccount objects? For example, what if you wanted to track the total number of bank accounts that have been created in your application? It wouldn't make sense for each BankAccount object to have its own separate counter. Instead, you need a single, shared counter that belongs to the BankAccount class itself. This is where the static keyword comes in.
A member (a field or a method) declared with the static keyword belongs to the class, not to an instance of the class.
The diagram below provides an excellent mental model for this concept. Notice how the instance variables (j, s2) are duplicated for each object (Obj1, Obj2, Obj3), while the class variables (i, s1) exist as a single, shared copy associated with the class itself.

Static Fields and Methods in Practice
To see how this works in practice, let's watch a detailed walkthrough. The following video uses a Cat class to demonstrate converting non-static members to static ones and explaining the reasoning behind it.
Static vs Non-Static Variables and Methods In Java - Full Simple Tutorial
This video from Coding with John provides a clear, step-by-step explanation of static vs. non-static members.
Please watch the following segments: Non-Static Review: This section reinforces the concepts from our last lesson, showing how non-static (instance) members belong to individual objects. Introducing Static: Here, the instructor introduces a static field (catCount) to track the number of Cat objects created. This is a classic and highly practical use case for a static variable. He also creates a static method (getCatCount) to access it. Key Rules and Best Practices: This part is crucial. It explains why you can't access non-static members from a static context and why you should always access static members using the class name (e.g., Cat.getCatCount()) rather than an object reference (e.g., myCat.getCatCount()).
The video brilliantly illustrates two key points:
- Static Fields are for data that is shared among all instances of a class. The
catCountis a perfect example. - Static Methods are often used as utility functions or to access static fields. They can be called directly on the class (
Cat.getCatCount()) without needing to create an object first.
Let's look at another common use case for static methods. The java.lang.Math class in Java is a classic example. It contains many static methods like Math.sqrt(), Math.max(), and Math.random(). You never need to create an instance of the Math class; you just call the methods you need directly:
double value = 16.0;
double squareRoot = Math.sqrt(value); // Calling a static method
System.out.println(squareRoot); // Prints 4.0
This makes sense because these are pure utility functions; their operation doesn't depend on the state of any particular object.
The "Shared" Nature of Static Fields
The most important property of a static field is that there is only one copy, which is shared by all objects. If one object changes the value of a static field, all other objects will see that change.
The following image provides a concise visual of this process. In the Counter class, x is a static variable. Each time a new Counter object is created, the constructor increments the same x.

To see this demonstrated in a live example, the next video shows exactly what happens when you modify a static variable through one of the object references.
This video from Telusko clearly demonstrates the shared memory aspect of static variables.
Watch the segment from static behavior. The instructor changes a static variable's value using obj1 and then shows that the change is reflected when he prints the value from obj2. He also reinforces the best practice of using the class name to access static members.
Static vs. Non-Static: A Summary
You now have a solid understanding of the difference between instance and class members. Let's formalize this with a direct comparison. The following article from Baeldung provides excellent, concise explanations.
A Guide to the Static Keyword in Java
This article provides a solid technical summary of static fields and methods.
Please read the following sections to reinforce the concepts: Skim the introduction. Read section 2, "The static Fields". The Car example with numberOfCars is another great illustration of a static counter. Read the first two paragraphs of section 3, "The static Methods", and the paragraph on utility classes.
Based on what we've learned, we can summarize the key differences in a table.
| Aspect | Static Member (Class Member) | Non-Static Member (Instance Member) |
|---|---|---|
| Belongs To | The class itself. | An individual object (instance) of the class. |
| Memory Allocation | Allocated once when the class is loaded into memory. | Allocated for each object that is created. |
| Access Method | Accessed using the class name (e.g., ClassName.member). |
Accessed using an object reference (e.g., objectName.member). |
| State | Shared across all objects of the class. | Each object has its own separate state. |
| Common Use Cases | Shared counters, constants, utility methods (Math.sqrt()). |
Properties unique to an object (e.g., name, balance, color). |
| Key Rule | Cannot directly access non-static members. | Can directly access both static and non-static members. |
Conclusion
In this lesson, you've learned how to differentiate between instance members and class-level members using the static keyword. This distinction is fundamental to good object-oriented design in Java.
Here are the essential takeaways:
- Non-static (instance) members belong to individual objects. Each object gets its own copy.
- Static (class) members belong to the class itself. There is only one shared copy for all objects.
- Use
staticfields for data that needs to be shared, like counters or constants. - Use
staticmethods for utility functions that don't depend on an object's state, or to access static fields. - A crucial rule to remember is that a static context cannot directly access instance members.
- The best practice is to always access static members using the class name (e.g.,
Math.sqrt()) for clarity.
In the "Coding with John" video, you saw a brief glimpse of the public static final combination used to create constants. In our next lesson, we will explore the final keyword in depth to understand how it can be used to create immutable fields, non-overridable methods, and non-extendable classes.