Java Non-Static Members In Hindi

📔 : Java 🔗

Class में बिना static keyword के define किये गए सभी Properties / Methods Non Static members होते हैं , By default ये Properties / Methods Non Static ही होते हैं, और इन्हे class object के साथ ही access किया जा सकता है।

Java Difference Between Static And Non Static Members

Static और Non Static में सबसे बड़ा difference यही होता है कि , Static Members (Property / Methods) Access करने के लिए हमें उस class का Object नहीं बनाना पड़ता है , simply Class Object और dot operator (.) का use करके हम directly Class की Static Property / Methods Access कर सकते हैं।

जबकि Non Static Members को access करने के लिए Class का Object बनाना ही पड़ेगा , बिना Object बनाये Non Static Members (Property / Methods) को access नहीं कर पाएंगे।

Java Non Static Member Example

File : NonStaticMember.java

CopyFullscreenClose FullscreenRun
public class NonStaticMember {
  // define non static method.
  void my_method() {
     System.out.println("Non Static Method");
  }
  
  // define non static variable.
  String message = "Hello";
 
  public static void main(String[] args) {
    // now access them with class object.
    NonStaticMember obj = new NonStaticMember();
    obj.my_method();
    System.out.println(obj.message);
  }
}
Output
javac NonStaticMember.java
java NonStaticMember
Non Static Method
Hello

Update property value

object instance के through आप non static class properties की values को update भी कर सकते हैं।

obj.message = "Value changed";
System.out.println(obj.message);

// Output :
Value changed

हालाँकि ध्यान रहे आप जिस object instance variable के लिए property value change कर रहे हैं , property की value सिर्फ उसी object के लिए change होगी किसी दुसरे object के लिए नहीं क्योंकि class का object एक दुसरे से independent होता है। अगर आप class का दूसरा object create करते हैं तो वहां आपको initialized value ही मिलेगी।

File : NonStaticMember.java

CopyFullscreenClose FullscreenRun
public class NonStaticMember {
  String message = "Initialized value";
 
  public static void main(String[] args) {
    // now create an object and update value.
    NonStaticMember obj1 = new NonStaticMember();
    obj1.message = "value updated for obj1";
    System.out.println(obj1.message);
    
    // again create an object.
    NonStaticMember obj2 = new NonStaticMember();
    System.out.println(obj2.message);
  }
}
Output
javac NonStaticMember.java
java NonStaticMember
value updated for obj1
Initialized value

example में देख कर आप समझ सकते हैं कि हर Object की अपनी अलग property values हो सकती है।

Java Access static members inside non-static method

static members को आप किसी भी non-static method में class name का use करके या directly easily access कर सकते हैं।
See Example :

File : A.java

CopyFullscreenClose FullscreenRun
public class A {
  // non static method.
  void my_method() {
     // call static method and vairables.
     A.staticMethod();
     System.out.println(A.message);
     
     // or you can also access directly.
     staticMethod();
     System.out.println(message);
  }
  
  // define static variable & method.
  static String message = "Static Property";
  static void staticMethod() {
    System.out.println("Static Method");
  }
 
  public static void main(String[] args) {
    A aobj = new A();
    aobj.my_method();
  }
}
Output
javac A.java
java A
Static Method
Static Property
Static Method
Static Property

Java Access non-static members inside static method

जैसा कि हम जानते हैं कि non-static members को access करने के लिए class object का use करते हैं तो किसी भी non-static method में static members को access करने के लिए simply उस class का object initialize करके कर सकते हैं।
For Example :

File : A.java

CopyFullscreenClose FullscreenRun
public class A {
  // static static method.
  static void my_method() {
     // call non method and vairables.
     A aobj = new A();
     aobj.NonStaticMethod();
     System.out.println(aobj.message);
  }
  
  // define non static variable & method.
  String message = "Non-Static Property";
  void NonStaticMethod() {
    System.out.println("Non-Static Method");
  }
 
  public static void main(String[] args) {
    //call static method.
    A.my_method();
  }
}
Output
javac A.java
java A
Non-Static Method
Non-Static Property

Java Access non-static members inside non-static method

अभी तक आपने non-static members को static method में access किया और static members को non-static method में access किया है। अब हम non-static members को non-static method में access करना सीखेंगे।

non-static method में non-static members को access करने के लिए आप simply class का object भी initiate कर सकते हैं और अगर access किये जाने वाले non-static members same class के हैं तो आप predefined keyword this का use भी कर सकते हैं।
For Example :

File : A.java

CopyFullscreenClose FullscreenRun
public class A {
  void my_method() {
     // using object.
     A aobj = new A();
     aobj.NonStaticMethod();
     System.out.println(aobj.message);
     
     // using this.
     this.NonStaticMethod();
     System.out.println(this.message);
  }
  
  String message = "Non-Static Property";
  void NonStaticMethod() {
    System.out.println("Non-Static Method");
  }
 
  public static void main(String[] args) {
    //call static method.
    A a = new A();
    a.my_method();
  }
}
Output
javac A.java
java A
Non-Static Method
Non-Static Property
Non-Static Method
Non-Static Property

ध्यान रहे कि आप किसी भी static method में this का use करके किसी भी non static member को access नहीं कर सकते हैं क्योंकि static methods में object bind नहीं होता है।

Java this keyword

Java programming language में this एक predefined keyword है जो कि current object को refer करता है , इसीलिए हम इसके though non static members को access कर पाते हैं। हालाँकि इसके बहुत सारे uses हैं जिन्हे आप आगे पढ़ेंगे।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers