Java के Constants
In this page:
final dataType CONSTANT_NAME = value;
The final Keyword
किसी variable को final मार्क करना इसकी value को पहले assignment के बाद lock कर देता है -- बाद में इसे reassign करने की कोई भी कोशिश एक compile-time error है, जो बिल्कुल वही safety guarantee है जो आप उन values के लिए चाहते हैं जो कभी नहीं बदलनी चाहिए।
उदाहरण: The final Keyword
public class Main {
public static void main(String[] args) {
final int MAX = 10;
// MAX = 20; // ILLEGAL - compile-time error, final can't be reassigned
System.out.println(MAX);
}
}
Login to try C/C++/Java/PHP code in the editor
Static Constants
final के साथ static जोड़ना (static final int MAX_USERS = 100;) का मतलब है constant किसी individual object के बजाय class खुद से belong करता है, इसलिए हर instance उस single value को share करता है बजाय हर एक को अपनी copy मिलने के।
उदाहरण: Static Constants
public class Main {
static final int MAX_USERS = 100; // belongs to the class, shared by every instance
public static void main(String[] args) {
System.out.println(Main.MAX_USERS);
}
}
Login to try C/C++/Java/PHP code in the editor
Constant Naming Conventions
ALL_CAPS_WITH_UNDERSCORES convention (जैसे MAX_LIMIT) विशुद्ध रूप से stylistic है, लेकिन यह Java code में इतना universal है कि एक देखते ही एक reader को बिना इसकी declaration जांचे तुरंत बता देता है 'यह value fixed है'।
उदाहरण: Constant Naming Conventions
public class Main {
static final int MAX_LIMIT = 50; // ALL_CAPS_WITH_UNDERSCORES signals "this is fixed"
public static void main(String[] args) {
System.out.println(MAX_LIMIT);
}
}
Login to try C/C++/Java/PHP code in the editor
Dynamic Initialization of Constants
एक final variable को declaration time पर एक literal assign होना ज़रूरी नहीं -- इसे runtime पर एक method call या expression से compute किया जा सकता है, जब तक इसे कभी पढ़े जाने से पहले बिल्कुल एक बार assign किया जाए।
उदाहरण: Dynamic Initialization of Constants
public class Main {
static int computeLimit() { return 42; }
public static void main(String[] args) {
final int limit = computeLimit(); // computed at runtime, assigned exactly once
System.out.println(limit);
}
}
Login to try C/C++/Java/PHP code in the editor
Constant Best Practices
Constants को किसी class के top के पास (या एक dedicated constants class में) group करना मतलब है जो भी बाद में एक limit या threshold tune करे बिल्कुल जानता है कहां देखना है, scattered magic numbers में खोजने के बजाय।
उदाहरण: Constant Best Practices
public class Main {
static final int MAX_USERS = 100;
static final int MIN_AGE = 18;
// Constants grouped at the top of the class instead of scattered as magic numbers
public static void main(String[] args) {
System.out.println(MAX_USERS + " " + MIN_AGE);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक value होने के बाद
finalvariable बदलने की कोशिश करना, जो एक compile error है। - यह मान लेना कि एक
finalobject बदल नहीं सकता, जब reference fixed है लेकिन object की contents अब भी बदल सकती हैं। - एक shared constant को
static finalन बनाना, इसलिए हर object अपनी copy पाता है।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: