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 as final , it becomes a constant . Once assigned, its value cannot be changed throughout the program. The value is usually assigned during declaration. Example – Final Variable class PhyTest { final int Minmarks = 35 ; // final variable declaration void run () { // Minmarks = 40; // ❌ This will cause a compile-time error System.out.println( "The subject is cleared with minimum marks: " + Minmarks); } public static void main (String args[]) { PhyTest obj = new PhyTest (); obj.run(); } } Output The subject is cleared with minimum marks: 35 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 dec...