'final'
Keyword
in Java
The 'final'keyword in Java is used
to restrict modification:
- **final variable**: Cannot be
reassigned after initial assignment
- **final method**: Cannot be
overridden by subclasses
- **final class**: Cannot be
subclassed
3.
Final Variable ReassignmentError
public class FinalVariableExample {
public static void main(String[] args) {
final int x = 10; // Declare and initialize a final
variable
x = 20; // Error: Cannot assign a value to final
variable 'x'
}
}
4.
Final Method OverrideError
class Animal {
public final void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
public void makeSound() { // Error: Cannot override
the final method from Animal
System.out.println("Bark");
}
}
5.
Final Class InheritanceError
final class Vehicle {
// class content
}
class Car extends Vehicle { // Error: Cannot subclass
final class 'Vehicle'
// class content
}
6.
'static' Keyword inJava
• The 'static' keyword in Java is used to define class-level members:
• - **Static Variable**: Shared among all instances of the class
• - **Static Method**: Can be called without creating an object of the class
• - **Static Block**: Executes once when the class is loaded
7.
Static Variable
class Counter{
static int count = 0; // static variable shared by all
instances
public void increment() {
count++; // increments the static variable
}
public void showCount() {
System.out.println("Count: " + count);
}
}
8.
Static Variable
public classMain {
public static void main(String[] args) {
Counter obj1 = new Counter();
Counter obj2 = new Counter();
Counter obj3 = new Counter();
obj1.increment();
obj1.showCount(); // Output: Count: 1
obj2.increment();
obj2.showCount(); // Output: Count: 2
obj3.increment();
obj3.showCount(); // Output: Count: 3
}}
9.
Static Method OverrideError
class Animal {
public static void makeSound() {
System.out.println("Animal sound");
}
}
class Dog extends Animal {
@Override
public void makeSound() { // Error: Static method in
Animal cannot be overridden in Dog
System.out.println("Bark");
}
}
10.
Static Class Nestedin Non-static Context
static class OuterClass { // Error: Only inner classes
can be declared static
public void display() {
System.out.println("Outer class display method");
}
}
11.
Final vs Static:Key Differences
- **final**:
- Immutability or prevention of change.
- Applied to variables, methods, and classes.
- **static**:
- Class-level association, shared across all instances.
- Applied to variables, methods, and blocks.
12.
Summary
In Java:
- **final**keyword is used to restrict changes (variables, methods, and
classes).
- **static** keyword is used for shared class-level elements accessible without
instance creation.
Use these keywords carefully to optimize memory usage and maintain code
stability.
#3 A final variable cannot be reassigned after its initial assignment.
Explanation: Since x is final, trying to assign a new value to it results in a compilation error.
#4 A final method cannot be overridden in a subclass
Explanation: Since makeSound() is declared final in the Animal class, attempting to override it in Dog results in a compilation error.
#5 A final class cannot be subclassed.
Explanation: The Vehicle class is final, so any attempt to extend it (as in Car) results in a compilation error.
#7 A static variable cannot be accessed directly within a non-static context (like an instance method) without referencing its class name.
Explanation: Since x is static, it should be accessed directly by its name (x) or by the class name (StaticVariableExample.x). Using this.x will cause a compilation error because this refers to the current instance, which isn't compatible with static variables.
#8 A static variable cannot be accessed directly within a non-static context (like an instance method) without referencing its class name.
Explanation: Since x is static, it should be accessed directly by its name (x) or by the class name (StaticVariableExample.x). Using this.x will cause a compilation error because this refers to the current instance, which isn't compatible with static variables.
#9 A static method cannot be overridden in the traditional sense; instead, it can only be hidden by another static method in the subclass.
Explanation: Attempting to override a static method results in an error. The method in Dog should also be declared as static to "hide" the one in Animal.
#10 A static keyword can only be applied to nested classes within a parent class. Top-level classes cannot be static.
Explanation: Declaring a top-level class as static is invalid in Java, leading to a compilation error. Only inner (nested) classes within another class can be declared as static.