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

Java के Non-Access Modifiers

static, final, और abstract जैसे Non-access modifiers एक class, method, या variable का behavior control करते हैं इसकी visibility नहीं, public या private जैसे access modifiers के विपरीत।
Syntax
java
static dataType field;
final dataType field = value;
abstract void methodName();
synchronized void methodName() { }

What are Non-Access Modifiers?

Non-access modifiers एक class, method, या variable का behavior control करते हैं इसकी visibility नहीं -- static, final, और abstract जैसे keywords यह बदलते हैं कि एक member कैसे काम करता है, public या private जैसे access modifiers के विपरीत जो control करते हैं कौन इसे देख सकता है।

उदाहरण: What are Non-Access Modifiers?

java
class Circle {
	static final double PI = 3.14159; // static and final change behavior, not visibility
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Circle.PI);
	}
}

The static Modifier

static modifier एक field या method को individual objects के बजाय class खुद से attach करता है, इसलिए एक static field सभी instances से shared है और एक static method को पहले object बनाए बिना call किया जा सकता है।

उदाहरण: The static Modifier

java
// Define the class Counter
class Counter {
	static int count = 0;
	static void increment() {
		count++;
	}
}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Counter.increment();
		Counter.increment();
		// Print a line to the console
		System.out.println(Counter.count);
	}
}

The final Modifier

final modifier आगे बदलाव रोकता है: एक final variable सिर्फ एक बार assign किया जा सकता है, एक final method किसी subclass से override नहीं हो सकती, और एक final class किसी भी दूसरी class से extend नहीं हो सकती।

उदाहरण: The final Modifier

java
final class Constants {
}
public class Main {
	public static void main(String[] args) {
		final int max = 100;
		// max = 200; // would not compile
		System.out.println(max);
	}
}

The abstract Modifier

abstract modifier एक class को सीधे instantiate न होने लायक मार्क करता है और एक method को कोई body न होने के रूप में मार्क करता है, हर concrete subclass को उस method का अपना implementation प्रदान करने की मांग करते हुए।

उदाहरण: The abstract Modifier

java
// Define the class Shape
abstract class Shape {
	abstract double area();
}
// Define the class Square
class Square extends Shape {
	// Declare side and set it to 4
	double side = 4;
	double area() {
		// Send side * side back to the caller
		return side * side;
	}
}
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Print a line to the console
		System.out.println(new Square().area());
	}
}

Combining Non-Access Modifiers

Non-access modifiers उसी member पर combine किए जा सकते हैं जब उनके meanings conflict न करें, जैसे एक shared, unchangeable constant के लिए static final, या abstract class के अंदर अलग contexts में एक साथ दिखने वाले abstract और static।

उदाहरण: Combining Non-Access Modifiers

java
class Config {
	static final int MAX_USERS = 100; // static + final together
}
public class Main {
	public static void main(String[] args) {
		System.out.println(Config.MAX_USERS);
	}
}
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. एक class या method पर abstract को final के साथ उपयोग करना, जो एक compile error है।
  2. एक method पर abstract को private या static के साथ combine करना, जो allowed नहीं।
  3. एक static method से एक non-static member call करना, जो एक 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.