OBJECT ORIENTED PROGRAMMING THROUGH JAVA : Unit - 2 : Topic - 17 : Attributes: Final and Static in Java
Unit - 2
Topic - 17 : Attributes: Final and Static in Java
1. Final in Java
The final
modifier can be used with variables, methods, and classes.
-
Final Variables
If a variable is declared asfinal
, it becomes a constant.
Once assigned, its value cannot be changed throughout the program.
The value is usually assigned during declaration.
Example – Final Variable
Output
Note:
Since Minmarks
is declared as final, attempting to modify it will result in a compile-time error.
2. Static Methods in Java
When a method is declared as static
, it becomes a class method.
-
It can be called without creating an object of the class.
-
Static methods belong to the class, not to individual objects.
-
Static methods can only access static variables directly.
Example – Static Method
Output
✅ Key Points:
-
final
variable → Constant, cannot be modified. -
static
method → Belongs to class, can be called without an object. -
Static methods can access only static variables directly.
Comments
Post a Comment