Java Class Fields vs Instance Fields

In this article, we will learn what is class fields, instance fields and difference between them with an example.

A field in Java is a variable that's declared inside a class. Fields represent data that's associated with classes and objects (instances of classes). Fields are essential for object-oriented programming, as they encapsulate the state information of objects.

Now, we know what field. Next, let's understand  class fields, instance fields in Java.

Instance Fields 

Instance fields, also known simply as non-static fields, belong to an instance of the class (i.e., an object). Their values are specific to that particular instance. Each object of the class will have its own copy of the instance fields.

Example:

public class Person {
    String name;  // instance field
    int age;      // instance field
    
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void displayInfo() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

    public static void main(String[] args) {
        Person person1 = new Person("Alice", 25);
        Person person2 = new Person("Bob", 30);

        person1.displayInfo();  // Output: Name: Alice, Age: 25
        person2.displayInfo();  // Output: Name: Bob, Age: 30
    }
}

In the above example, name and age are instance fields. Each Person object has its own separate copies of these fields.

Class Fields (Static Fields) 

Class fields, also known as static fields, belong to the class itself rather than any particular instance. This means there's only one copy of a static field, and it's shared among all instances of the class. 

Example:

public class Calculator {
    static final double PI = 3.14159265359;  // class field

    public double calculateArea(double radius) {
        return PI * radius * radius;
    }

    public static void main(String[] args) {
        Calculator calculator1 = new Calculator();
        Calculator calculator2 = new Calculator();

        System.out.println(Calculator.PI);  // Output: 3.14159265359
        System.out.println(calculator1.PI); // Output: 3.14159265359
        System.out.println(calculator2.PI); // Output: 3.14159265359
    }
}

In the above example, PI is a class field (denoted by the static keyword). Regardless of how many Calculator objects are created, there's only one copy of PI.

Key Differences 

Memory Usage: 

Instance Fields: Every object instance has its own copy. If you create 100 objects, you'll have 100 copies of each instance field. 

Class Fields: Only one copy exists regardless of the number of objects created. 

Access: 

Instance Fields: Can be accessed directly only from non-static methods. Outside the class, they're accessed with an object reference. 

Class Fields: Can be accessed from both static and non-static methods. They can also be accessed using the class name or directly within the class. 

Modification: 

Instance Fields: Modifying the field in one object does not affect other objects. 

Class Fields: Since there's only one copy, modifying the field affects all objects of the class. 

Conclusion 

Understanding the difference between instance fields and class fields is fundamental in Java. While instance fields encapsulate the state of individual objects, class fields store data related to the class itself, shared across instances. Using them judiciously will lead to cleaner, more efficient, and more predictable code.

Comments