Java की final Keyword
final dataType CONSTANT = value;
final returnType methodName() { }
final class ClassName { }
Final Variables
एक final variable बिल्कुल एक बार assign किया जा सकता है; उस पहले assignment के बाद, इसे reassign करने की कोई भी कोशिश एक compile error है। यह language level पर एक असली constant express करने का Java का तरीका है, सिर्फ एक naming convention नहीं।
उदाहरण: Final Variables
public class Main {
public static void main(String[] args) {
final int max = 100;
System.out.println(max);
// max = 200; // compile error: reassigning a final variable
}
}
Login to try C/C++/Java/PHP code in the editor
Blank Final Variables
एक blank final variable declaration के point पर बिना initializer के declared है, लेकिन Java अब भी चाहता है कि यह बिल्कुल एक बार assign किया जाए — आमतौर पर हर constructor के अंदर — object को पूरी तरह constructed मानने से पहले।
उदाहरण: Blank Final Variables
class Config {
final int id;
Config(int id) {
this.id = id; // must be assigned exactly once, here in the constructor
}
}
public class Main {
public static void main(String[] args) {
Config c = new Config(7);
System.out.println(c.id);
}
}
Login to try C/C++/Java/PHP code in the editor
Final Methods
एक method को final मार्क करना किसी subclass को इसे override करने से रोकता है, जो तब उपयोगी है जब एक method ऐसी logic implement करता है (जैसे एक security check या एक core algorithm) जिसे हर subclass में बिना exceptions identically behave करना चाहिए।
उदाहरण: Final Methods
class Vehicle {
final void checkSafety() {
System.out.println("Safety check passed");
}
}
class Car extends Vehicle {
// void checkSafety() {} // would not compile: cannot override a final method
}
public class Main {
public static void main(String[] args) {
new Car().checkSafety();
}
}
Login to try C/C++/Java/PHP code in the editor
Final Classes
एक final class को बिल्कुल extend नहीं किया जा सकता — इससे कोई subclass नहीं बनाई जा सकती।
String खुद standard library में final है, specifically ताकि String की immutability guarantees पर निर्भर code को एक malicious या careless subclass से तोड़ा न जा सके।
उदाहरण: Final Classes
final class Constants {
static final double PI = 3.14159;
}
// class MoreConstants extends Constants {} // would not compile: Constants is final
public class Main {
public static void main(String[] args) {
System.out.println(Constants.PI);
}
}
Login to try C/C++/Java/PHP code in the editor
Final Parameters
एक method parameter को final declare करना method body को उस parameter को एक अलग reference या value reassign करने से रोकता है, हालांकि यह इस बारे में कुछ नहीं कहता कि क्या parameter जिस object की ओर point करता है वह खुद internally mutate हो सकता है।
उदाहरण: Final Parameters
public class Main {
static void show(final int x) {
// x = 10; // would not compile: x is final
System.out.println(x);
}
public static void main(String[] args) {
show(5);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक
finalvariable reassign करने की कोशिश करना, जो एक compile error है। - एक subclass में एक
finalmethod override करना, जो एक compile error है। Stringजैसी एकfinalclass extend करना, जो एक compile error है।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: