Hello दोस्तों! आज इस पोस्ट में आपको Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है? को पढ़ेंगे,, इस पोस्ट को आप पूरा पढ़िए. इस article को बहुत ही आसान और सरल भाषा में लिखा गया है. यह article आपके exam के लिए बहुत उपयोगी साबित होगी. जो छात्र परीक्षा की तैयारी रहे हैं वो सभी इस article (C लैंग्वेज क्या है C language in hindi )की मदद से आप सभी आसानी से हिंदी भाषा में सीख सकते हैं |
Table of Contents
Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है?
नेस्टेड क्लास जब हम एक क्लास को दूसरे क्लास के अंदर परिभाषित करते हैं, तो हम इसे नेस्टेड क्लास कहते हैं। इसका मतलब है कि एक क्लास के अंदर एक और क्लास हो सकता है। नेस्टेड क्लास को प्राइवेट, प्रोटेक्टेड, पब्लिक या पैकेज प्राइवेट तरीके से प्राचलित किया जा सकता है। इसका उपयोग बाहरी क्लास के साथ संबंधित कोड को अधिक संरचित बनाने के लिए किया जाता है।
नेस्टेड क्लास के प्रकार – Types of nested class in java in hindi
नेस्टेड क्लास के तीन मुख्य प्रकार होते हैं |
स्टैटिक नेस्टेड क्लास ( Static nested class )
स्थानीय आंतर क्लास (Local Inner Class )
स्थानीय आंतर क्लास (Local Inner Class)
स्टैटिक नेस्टेड क्लास ( Static nested class in java in hindi )
यह एक स्टैटिक क्लास को एक अन्य क्लास के अंदर परिभाषित करता है और सामान्य बाहरी क्लास के तरह प्राइवेट वस्तुओं तक पहुँचने की अनुमति देता है।
public class OuterClass { // This is an instance variable in the outer class private int outerVar = 10; // This is a static nested class static class StaticNestedClass { // This is a static nested class variable private int nestedVar = 5; void display() { System.out.println("Value of nestedVar: " + nestedVar); } } public static void main(String[] args) { // Create an instance of the outer class OuterClass outer = new OuterClass(); // Create an instance of the static nested class StaticNestedClass nested = new StaticNestedClass(); // Access the members of the static nested class nested.display(); // Access the instance variable of the outer class System.out.println("Value of outerVar: " + outer.outerVar); } } |
“OuterClass” में हम एक बाहरी चरण “outerVar” घोषित कर रहे हैं, जिसका मूल्य 10 है।
“StaticNestedClass” एक स्थैतिक नेस्टेड कक्षा है और इसमें हम एक स्थैतिक नेस्टेड कक्षा चरण “nestedVar” घोषित कर रहे हैं, जिसका मूल्य 5 है।
“StaticNestedClass” के भीतर हम “display” नामक एक चरण भी दिखा रहे हैं, जिसमें हम “nestedVar” की मूल्य प्रिंट कर रहे हैं।
“main” चरण में हम “OuterClass” का एक उदाहरण बना रहे हैं और “StaticNestedClass” का एक उदाहरण बना रहे हैं। फिर हम स्थैतिक नेस्टेड कक्षा के सदस्यों का पहुँच कर उनका प्रदर्शन कर रहे हैं और बाहरी कक्षा के उदाहरण चरण का उपयोग करके “outerVar” की मूल्य को प्रिंट कर रहे हैं।
यह कोड बाहरी और स्थैतिक नेस्टेड कक्षाओं का उपयोग करके दिखाता है कि कैसे इनका उपयोग किया जाता है और उनके सदस्यों तक कैसे पहुँचा जाता है।
OutPut
Value of nestedVar: 5 Value of outerVar: 10 |
गैर-स्टैटिक नेस्टेड क्लास या Inner Class (Non-Static Nested Class in Hindi)
इसे एक बाहरी क्लास के अंदर परिभाषित किया जाता है और यह उस क्लास के सभी सदस्यों तक पहुँचने की अनुमति देता है।
Example
public class OuterClass { // This is an instance variable in the outer class private int outerVar = 10; // This is an inner class (non-static nested class) class InnerClass { // This is an instance variable in the inner class private int innerVar = 5; void display() { System.out.println("Value of innerVar: " + innerVar); System.out.println("Value of outerVar: " + outerVar); // Accessing outer class variable } } public static void main(String[] args) { // Create an instance of the outer class OuterClass outer = new OuterClass(); // Create an instance of the inner class InnerClass inner = outer.new InnerClass(); // Access the members of the inner class inner.display(); } } |
Output
Value of innerVar: 5 Value of outerVar: 10 |
स्थानीय आंतर क्लास (Local Inner Class in hindi)
इसे एक ब्लॉक के अंदर परिभाषित किया जाता है, जैसे कि एक मेथड के अंदर, और यह उस ब्लॉक के बाहरी कोड तक पहुँचने की अनुमति नहीं देता है।
Example
public class OuterClass { private int outerVar = 10; public void outerMethod() { // This is a local inner class defined within the outerMethod class LocalInnerClass { private int innerVar = 5; void display() { System.out.println("Value of innerVar: " + innerVar); System.out.println("Value of outerVar: " + outerVar); // Accessing outer class variable } } // Create an instance of the local inner class LocalInnerClass inner = new LocalInnerClass(); // Access the members of the local inner class inner.display(); } public static void main(String[] args) { // Create an instance of the outer class OuterClass outer = new OuterClass(); // Call the outer method that defines and uses the local inner class outer.outerMethod(); } } |
“main” मेथड में, हम बाहरी कक्षा “OuterClass” का एक उदाहरण बनाते हैं और “outerMethod” मेथड को कॉल करते हैं, जिसमें स्थानीय आंतरिक कक्षा को परिभाषित किया जाता है और उसका उपयोग किया जाता है।
Output
Value of innerVar: 5 Value of outerVar: 10 |
Nested Class in Java के फायदे
- Nested Class in Java से encapsulation बढ़ता है, यानि कि outer class के members को inner class से ही access किया जा सकता है, और inner class को outer class से ही create किया जा सकता है।
- Nested Class in Java से code readability और maintainability में सुधार होता है, क्योंकि nested class को logically group किया जा सकता है, और nested class का use सिर्फ उसी place पर होता है, जहाँ पर वह define हुआ है।
- Nested Class in Java से inheritance में flexibility मिलती है, क्योंकि nested class को outer class से independent interface implement करने में मदद मिलती है, और nested class outer class के methods में parameters की तरह use हो सकती है।
Nested Class in Java के नुकसान
- Nested Class in Java से code complexity बढ़ सकती है, क्योंकि nested class outer class के scope में bound (घिरा) होती है, और nested class outer class के members (private members भी) को access कर सकती है, जिससे confusion create हो सकता है।
- Nested Class in Java से memory consumption बढ़ सकता है, क्योंकि nested class outer class का reference hold (रखती) होती है, और nested class outer class के objects (instances) create (बनाने) पर memory allocate (प्रदान) होती है, जिससे memory leak (नुकसान) हो सकता है।
- Nested Class in Java से performance degradation (कमी) हो सकता है, क्योंकि nested class outer class के methods call (पुकारने) पर load (लोड) होती है, और nested class outer class के members access (पहुंचने) पर extra overhead (मेहनत) होती है, जिससे execution time (समय) increase (बढ़) सकता है।
You Might also like…… Operator in java, जावा मे operators क्या है ? Operators कितने प्रकार के होते है ?
Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है? Faqs
स्टैटिक नेस्टेड क्लास क्या होता है?
स्टैटिक नेस्टेड क्लास एक अंतर्निहित वर्ग है जो एक अन्य क्लास के भीतर स्थायी रूप से परिभाषित किया जाता है
स्टैटिक नेस्टेड क्लास क्या होता है?
स्टैटिक नेस्टेड क्लास एक जटिलता भरे जगह है जिसमें हम स्टैटिक नेस्टेड क्लास के बारे में मूल जानकारी प्राप्त करते हैं।
CONCLUSION :-
आज के इस आर्टिकल मे हमने Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है? के बारे मे विस्तार से जाना आशा है की यह ARTICAL (Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है?) आप के लिए HELPFUL रहा होगा | अगर यह ARTICAL आप को पसंद आया हो तो इसे अपने दोस्तों के साथ SHARE जरूर करे | आप हमे COMMENT के माध्यम से सुझाव दे सकते है आप हमे Email-id [email protected] पर अपने सुझाव दे सकते है |
Nested Class in Java in Hindi – जावा में नेस्टेड क्लास क्या है?