Static and Final Keywords

Thinking

The static and final keywords in Java are fundamental modifiers that control how variables, methods, and classes behave in terms of memory, inheritance, and mutability.

Mastering these helps in writing efficient, clear, and secure Java programs.

Description

Static Keyword:

  • Used to declare class-level variables and methods, which means they belong to the class rather than any instance.
  • Static members are shared among all instances of the class.
  • Commonly used for utility methods and constants.

Final Keyword:

  • Used to declare constants, methods that cannot be overridden, and classes that cannot be subclassed.
  • A final variable’s value cannot be changed once assigned.
  • Ensures immutability and prevents unintended behavior.

Combining static and final is commonly used to define constants.

Video Resources

Java Static Keyword Tutorial

Understand how static variables and methods work in Java.

Examples (code)

Static Keyword Example


// Static variable and method example
class Counter {
    static int count = 0;

    Counter() {
        count++; // Increment count for every new object
    }

    static void displayCount() {
        System.out.println("Count: " + count);
    }
}

public class Main {
    public static void main(String[] args) {
        new Counter();
        new Counter();
        Counter.displayCount();  // Output: Count: 2
    }
}
  

Final Keyword Example


// Final variable and method example
class Constants {
    final int MAX_VALUE = 100;

    final void display() {
        System.out.println("This is a final method.");
    }
}

class SubClass extends Constants {
    // This would cause an error if uncommented:
    // void display() {
    //    System.out.println("Cannot override final method");
    // }
}

public class Main {
    public static void main(String[] args) {
        Constants obj = new Constants();
        System.out.println(obj.MAX_VALUE); // Output: 100
        obj.display();
    }
}
  

Static Final Combination Example


// Static final variable example (constant)
class MathConstants {
    static final double PI = 3.14159;
}

public class Main {
    public static void main(String[] args) {
        System.out.println(MathConstants.PI); // Output: 3.14159
    }
}
  

Real-World Applications

Utility Classes

Static methods are often used in utility/helper classes like Math and Collections.

Constants Management

Using static final variables to define constants helps maintain consistent values across the application.

Immutable Designs

Final variables help create immutable objects and prevent unwanted changes.

Interview Questions

Q1: What does the static keyword do in Java?

Show Answer

The static keyword makes a variable or method belong to the class rather than instances of the class.

Q2: Can you override a final method?

Show Answer

No, a final method cannot be overridden by subclasses.

Q3: Why use static final variables?

Show Answer

static final variables are constants that are shared among all instances and cannot be changed after initialization.