In Java, the static
and final
keywords are used to define specific behaviors and constraints for variables, methods, and classes. Let’s explore each keyword in detail:
1. static
Keyword
The static
keyword is used to define members (variables, methods, and blocks) that belong to the class itself rather than to any specific instance of the class. This means they can be accessed without creating an object of the class.
a. Static Variables
- Also called class variables.
- Shared across all instances of the class.
- Memory is allocated only once, at the time of class loading.
- Example:
public class Counter {
static int count = 0; // Static variable
Counter() {
count++; // Increment count each time an object is created
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
System.out.println("Total objects created: " + Counter.count); // Output: 2
}
}
b. Static Methods
- Belong to the class, not to any instance.
- Can only access static variables and call other static methods.
- Cannot use
this
orsuper
keywords. - Example:
public class MathUtils {
static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = MathUtils.add(5, 10); // Call static method without creating an object
System.out.println("Result: " + result); // Output: 15
}
}
c. Static Block
- Used to initialize static variables.
- Executed when the class is loaded into memory.
- Example:
public class MyClass {
static int x;
static {
x = 10; // Static block to initialize x
System.out.println("Static block executed.");
}
public static void main(String[] args) {
System.out.println("Value of x: " + x); // Output: 10
}
}
d. Static Nested Class
- A nested class declared as
static
. - Can be accessed without creating an instance of the outer class.
- Example:
public class OuterClass {
static class NestedClass {
void display() {
System.out.println("Inside static nested class.");
}
}
public static void main(String[] args) {
OuterClass.NestedClass obj = new OuterClass.NestedClass(); // No need for OuterClass instance
obj.display();
}
}
2. final
Keyword
The final
keyword is used to restrict modification of variables, methods, and classes.
a. Final Variables
- Once assigned a value, it cannot be changed.
- Used to define constants.
- Example:
public class Circle {
final double PI = 3.14159; // Final variable (constant)
double calculateArea(double radius) {
return PI * radius * radius;
}
public static void main(String[] args) {
Circle circle = new Circle();
System.out.println("Area: " + circle.calculateArea(5)); // Output: 78.53975
}
}
b. Final Methods
- Cannot be overridden by subclasses.
- Example:
class Parent {
final void display() {
System.out.println("This is a final method.");
}
}
class Child extends Parent {
// Cannot override display() method
}
c. Final Classes
- Cannot be extended (inherited).
- Example:
final class MyFinalClass {
void display() {
System.out.println("This is a final class.");
}
}
// Cannot extend MyFinalClass
// class SubClass extends MyFinalClass { } // Error
3. Combining static
and final
- A
static final
variable is a constant that belongs to the class. - Commonly used for defining global constants.
- Example:
public class Constants {
static final double PI = 3.14159; // Static final variable (constant)
public static void main(String[] args) {
System.out.println("Value of PI: " + Constants.PI); // Access without object
}
}
4. Key Differences
Feature | static | final |
---|---|---|
Purpose | Belongs to the class, not instances | Restricts modification or inheritance |
Variables | Shared across all instances | Value cannot be changed |
Methods | Can be called without an object | Cannot be overridden |
Classes | N/A | Cannot be inherited |
5. Example Combining Both
public class Example {
static final int MAX_VALUE = 100; // Static final variable
static void display() {
System.out.println("Max value: " + MAX_VALUE);
}
public static void main(String[] args) {
Example.display(); // Call static method
}
}
By understanding static
and final
, you can write more efficient, secure, and maintainable Java code!