← Back to Core Java Course | Chapter 1: Introduction & Basics | Lesson 11 of 12

Java के Constants

एक constant वह value है जिसे आप कभी न बदलने का वादा करते हैं, जैसे हफ्ते में दिनों की संख्या। Java में आप इसे final शब्द से मार्क करते हैं।
Syntax
java
final dataType CONSTANT_NAME = value;

The final Keyword

किसी variable को final मार्क करना इसकी value को पहले assignment के बाद lock कर देता है -- बाद में इसे reassign करने की कोई भी कोशिश एक compile-time error है, जो बिल्कुल वही safety guarantee है जो आप उन values के लिए चाहते हैं जो कभी नहीं बदलनी चाहिए।

उदाहरण: The final Keyword

java
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);
	}
}

Static Constants

final के साथ static जोड़ना (static final int MAX_USERS = 100;) का मतलब है constant किसी individual object के बजाय class खुद से belong करता है, इसलिए हर instance उस single value को share करता है बजाय हर एक को अपनी copy मिलने के।

उदाहरण: Static Constants

java
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);
	}
}

Constant Naming Conventions

ALL_CAPS_WITH_UNDERSCORES convention (जैसे MAX_LIMIT) विशुद्ध रूप से stylistic है, लेकिन यह Java code में इतना universal है कि एक देखते ही एक reader को बिना इसकी declaration जांचे तुरंत बता देता है 'यह value fixed है'।

उदाहरण: Constant Naming Conventions

java
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);
	}
}

Dynamic Initialization of Constants

एक final variable को declaration time पर एक literal assign होना ज़रूरी नहीं -- इसे runtime पर एक method call या expression से compute किया जा सकता है, जब तक इसे कभी पढ़े जाने से पहले बिल्कुल एक बार assign किया जाए।

उदाहरण: Dynamic Initialization of Constants

java
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);
	}
}

Constant Best Practices

Constants को किसी class के top के पास (या एक dedicated constants class में) group करना मतलब है जो भी बाद में एक limit या threshold tune करे बिल्कुल जानता है कहां देखना है, scattered magic numbers में खोजने के बजाय।

उदाहरण: Constant Best Practices

java
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);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक value होने के बाद final variable बदलने की कोशिश करना, जो एक compile error है।
  2. यह मान लेना कि एक final object बदल नहीं सकता, जब reference fixed है लेकिन object की contents अब भी बदल सकती हैं।
  3. एक shared constant को static final न बनाना, इसलिए हर object अपनी copy पाता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.