← Back to Core Java Course | Chapter 7: OOP Core | Lesson 9 of 11

Java की final Keyword

final का मतलब है 'यह बदला नहीं जा सकता', एक permanent marker में लिखने जैसा। यह एक variable, एक method, या एक पूरी class lock कर सकता है।
Syntax
java
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

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

Blank Final Variables

एक blank final variable declaration के point पर बिना initializer के declared है, लेकिन Java अब भी चाहता है कि यह बिल्कुल एक बार assign किया जाए — आमतौर पर हर constructor के अंदर — object को पूरी तरह constructed मानने से पहले।

उदाहरण: Blank Final Variables

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

Final Methods

एक method को final मार्क करना किसी subclass को इसे override करने से रोकता है, जो तब उपयोगी है जब एक method ऐसी logic implement करता है (जैसे एक security check या एक core algorithm) जिसे हर subclass में बिना exceptions identically behave करना चाहिए।

उदाहरण: Final Methods

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

Final Classes

एक final class को बिल्कुल extend नहीं किया जा सकता — इससे कोई subclass नहीं बनाई जा सकती।

String खुद standard library में final है, specifically ताकि String की immutability guarantees पर निर्भर code को एक malicious या careless subclass से तोड़ा न जा सके।

उदाहरण: Final Classes

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

Final Parameters

एक method parameter को final declare करना method body को उस parameter को एक अलग reference या value reassign करने से रोकता है, हालांकि यह इस बारे में कुछ नहीं कहता कि क्या parameter जिस object की ओर point करता है वह खुद internally mutate हो सकता है।

उदाहरण: Final Parameters

java
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);
	}
}
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. एक final variable reassign करने की कोशिश करना, जो एक compile error है।
  2. एक subclass में एक final method override करना, जो एक compile error है।
  3. String जैसी एक final class extend करना, जो एक compile error है।

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.