Instance vs Static
Difference between instance and class members
5 min read
Instance vs Static Members
Understanding when to use instance members (belong to objects) vs static members (belong to class) is crucial for proper OOP design.
Comparison
| Aspect | Instance | Static |
|---|---|---|
| Belongs to | Object | Class |
| Memory | Heap (per object) | Method Area (once) |
| Access | object.member | Class.member |
| Uses this | Yes | No |
🔑 Rule of Thumb: If the data/behavior is unique to each object, use instance. If shared across all objects, use static.
Code Examples
Instance vs static in a single class
java
1public class Employee {
2 // STATIC - shared by all employees
3 private static int totalCount = 0; // Total employees
4 private static String companyName = "TechCorp";
5
6 // INSTANCE - unique to each employee
7 private int id;
8 private String name;
9 private double salary;
10
11 public Employee(String name, double salary) {
12 this.name = name;
13 this.salary = salary;
14 this.id = ++totalCount; // Assign unique ID
15 }
16
17 // Instance method - can access both
18 public void display() {
19 System.out.println("ID: " + id); // Instance
20 System.out.println("Name: " + name); // Instance
21 System.out.println("Company: " + companyName); // Static
22 }
23
24 // Static method - can only access static
25 public static int getTotalCount() {
26 return totalCount;
27 // return name; // ERROR! Can't access instance
28 }
29}
30
31Employee e1 = new Employee("Alice", 50000);
32Employee e2 = new Employee("Bob", 60000);
33Employee e3 = new Employee("Charlie", 55000);
34
35System.out.println(Employee.getTotalCount()); // 3 (shared)
36e1.display(); // Each has unique id, nameAccess rules for instance and static contexts
java
1// Access Rules Summary
2public class AccessRules {
3 private int instanceVar = 10;
4 private static int staticVar = 20;
5
6 // Instance method - full access
7 public void instanceMethod() {
8 System.out.println(instanceVar); // ✓ Instance var
9 System.out.println(staticVar); // ✓ Static var
10 staticMethod(); // ✓ Static method
11 anotherInstance(); // ✓ Instance method
12 System.out.println(this.instanceVar); // ✓ Uses this
13 }
14
15 // Static method - limited access
16 public static void staticMethod() {
17 // System.out.println(instanceVar); // ✗ ERROR!
18 System.out.println(staticVar); // ✓ Static var
19 anotherStatic(); // ✓ Static method
20 // anotherInstance(); // ✗ ERROR!
21 // System.out.println(this); // ✗ ERROR!
22
23 // To access instance, need object
24 AccessRules obj = new AccessRules();
25 System.out.println(obj.instanceVar); // ✓ Via object
26 }
27
28 private void anotherInstance() {}
29 private static void anotherStatic() {}
30}When to use static vs instance
java
1// When to Use Static vs Instance
2
3// USE STATIC FOR:
4public class MathUtils {
5 public static final double PI = 3.14159; // Constants
6
7 public static int add(int a, int b) { // Utility methods
8 return a + b;
9 }
10
11 private static int counter = 0; // Shared state
12
13 public static int getNextId() { // Factory helpers
14 return ++counter;
15 }
16}
17
18// USE INSTANCE FOR:
19public class BankAccount {
20 private String accountNumber; // Object-specific data
21 private double balance;
22
23 public void deposit(double amount) { // Object behavior
24 this.balance += amount;
25 }
26
27 public void transfer(BankAccount target, double amount) {
28 this.balance -= amount; // Uses this instance
29 target.balance += amount; // Modifies target instance
30 }
31}
32
33// COMMON PATTERN: Instance + Static together
34public class User {
35 private static int nextId = 1; // Static counter
36
37 private final int id; // Instance ID
38 private String name;
39
40 public User(String name) {
41 this.id = nextId++; // Use static, store in instance
42 this.name = name;
43 }
44}main() method and initialization order
java
1// main() is static - why?
2public class MainExample {
3 private String message = "Hello";
4
5 public static void main(String[] args) {
6 // main is static because JVM calls it without creating an object
7 // It's the entry point before any objects exist
8
9 // Cannot access instance members directly
10 // System.out.println(message); // ERROR!
11
12 // Must create object first
13 MainExample obj = new MainExample();
14 System.out.println(obj.message); // OK!
15
16 obj.instanceMethod(); // OK!
17 }
18
19 public void instanceMethod() {
20 System.out.println(message); // OK - has 'this'
21 }
22}
23
24// Static blocks vs Instance blocks
25public class InitializationOrder {
26 // Static block - runs once when class loads
27 static {
28 System.out.println("1. Static block");
29 }
30
31 // Instance block - runs each time object created
32 {
33 System.out.println("2. Instance block");
34 }
35
36 public InitializationOrder() {
37 System.out.println("3. Constructor");
38 }
39}
40
41// Order: Static block → Instance block → ConstructorUse Cases
- Instance: Object-specific state and behavior
- Static: Utility methods and constants
- Static: Counters and shared configuration
- Static: Factory methods
- Instance: Domain objects and entities
- Static: Singleton access methods
Common Mistakes to Avoid
- Accessing instance from static without object
- Using static when instance is needed
- Too many static variables (globals)
- Static methods reducing testability
- Forgetting main() is static
- Static mutable state causing thread issues