OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 1 : Static Variables and Methods, Attribute Final
Static Variables and Methods, Final Keyword in Java
In Java, the keywords static
and final
are used to control the behavior and memory management of variables and methods. These concepts are essential for understanding Java class-level features and constants.
static
Keyword in Java
The static
keyword is used to indicate that a variable or method belongs to the class rather than to instances (objects) of the class.
Features of Static:
- Static Variable: Shared by all objects of the class
- Static Method: Can be called without creating an object
- Memory-efficient: Allocated once per class
Static Variable
Declared using the keyword static
. It is common to all instances of the class.
class Counter {
static int count = 0;
Counter() {
count++;
System.out.println("Count is: " + count);
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
}
}
Output:
Count is: 1 Count is: 2 Count is: 3
Explanation:
The static variable count
is shared among all objects, so each object increments the same counter.
Static Method
A method declared as static
can be called without creating an object. It can only access static
data.
class Utility {
static void greet() {
System.out.println("Welcome to Java!");
}
public static void main(String[] args) {
Utility.greet(); // no need to create object
}
}
Output:
Welcome to Java!
Rules:
- Static methods can access only static data.
- They cannot use
this
orsuper
. - Used for utility or helper methods (e.g.,
Math.pow()
).
final
Keyword in Java
The final
keyword is used to make a variable, method, or class unchangeable or non-overridable.
Usage of final:
- Final Variable: Constant, cannot be reassigned
- Final Method: Cannot be overridden by subclasses
- Final Class: Cannot be inherited
Final Variable
class Constant {
final int MAX = 100;
void show() {
// MAX = 200; // Error: Cannot assign a value to final variable
System.out.println("MAX is: " + MAX);
}
public static void main(String[] args) {
Constant obj = new Constant();
obj.show();
}
}
Output:
MAX is: 100
Final Method
class Parent {
final void display() {
System.out.println("Final method in parent class.");
}
}
class Child extends Parent {
// void display() {} // Error: Cannot override final method
}
Final Class
final class A {
void show() {
System.out.println("Final class A.");
}
}
// class B extends A {} // Error: Cannot inherit from final class
Comparison: static
vs. final
Aspect | static |
final |
---|---|---|
Meaning | Belongs to class | Cannot be changed |
Variable | Shared across instances | Value can't be reassigned |
Method | Can be called without object | Cannot be overridden |
Class | Not applicable | Cannot be extended |
Summary
- Use
static
for shared class-level functionality. - Use
final
to protect constants, methods, or classes from modification. - They can be used together (e.g.,
public static final double PI = 3.14;
).
Comments
Post a Comment