Java “static” Keyword in Hindi

Java “static” Keyword


जावा लैंग्वेज में static keyword का उपयोग मुख्य रूप से memory management के लिए किया जाता है।  इसका प्रयोग variables, methods, blocks और nested classes के साथ किया जा सकता है।
इनके बारे में देखे तो instance variable जब class में declare किया जाता है, तब उस class का object या instance बनाने के बाद उसकी copy; object पर होती है | अगर एक class के multiple objects हो तो हर object पर instance variable की अलग-अलग copies होती है |

Static हो सकता है |

  1. Variable (also known as a class variable)
  2. Method (also known as a class method)
  3. Block
  4. Nested class

Java static/class variable

  • जब किसी variable को static keyword के साथ declare करते हैं तो उसे static variable कहते है।
  • static variables को Class Variable कहते है |क्योकि इनको केवल class level पर ही create कर सकते हैं |
  • Static Variable के लिए object; create करने की जरुरत नहीं होती  | क्योकि Static Variable को class के नाम से access किया जाता है |
  • Static Variable को हम Instance Variable जैसा उपयोग कर सकते है, लेकिन Instance Variable को Static Variable जैसा इस्तेमाल नहीं कर सकते |
Example : 
int age;               // Instance Variable
static int number;     // Static/Class Variable

Syntax for Accessing Static Variable :

//Accessing for Instance Variable by Class Instance/Object
class_instance.variable_name = variable_value;
Example : 
obj.a = 5;

//Accessing for Static/Class Variable by class name 
class_name.static_variable_name = static_variable_value;
Example : 
Sample.b = 10;

program of Static\class  Variable :

class Student{  
   int rollno;                  //instance variable  
   String name;  
   static String college ="XYZ";      //static variable  
  
   Student(int r, String n){  
   rollno = r;  
   name = n;  
   }  
   //method to display the values  
   void display (){
System.out.println(rollno+" "+name+" "+college);
}  
}  
//Test class to show the values of objects  
public class TestStaticVariable1{  
 public static void main(String args[]){  
 Student s1 = new Student(20201,"sachin");  
 Student s2 = new Student(20202,"amit");  
 //we can change the college of all objects by the single line of code  
 //Student.college="vwxyz";  
 s1.display();  
 s2.display();  
 }  
}  

OUTPUT : 
20201 sachin xyz
20202 amit xyz

Java static/class method

  • जब किसी method में static keyword को  apply किया जाता है तो उसे static/class method कहते है |
  • Static Method के लिए object; create करने की जरुरत नहीं होती | क्योकि Static Method को class के नाम से access किया जाता है |
  • static methods केवल दूसरे static methods को direct (सीधे) call कर सकते हैं |
  • Static Method को हम Instance Method जैसा उपयोग कर सकते है, लेकिन Instance Method को Static Method जैसा उपयोग नहीं कर सकते |
Example : 
int disp(){    }; // Instance Method
static int show(){   }; // Static/Class Method

Syntax for Accessing Static Method : 

//Accessing for Instance Method by Class Instance/Object
class_instance.instance_method_name();
Example :  
obj.disp();
//Accessing for Static/Class Method by class name 
class_name.static_method_name();
Example :
Sample.show();

program of Static\class  Method:

class Student{  
     int rollno;  
     String name;  
     static String college = "xyz";  
     //static method to change the value of static variable  
     static void change(){  
     college = "vwxyz";  
     }  
     Student(int r, String n){  
     rollno = r;  
     name = n;  
     }  
     //method to display values  
     void display(){
System.out.println(rollno+" "+name+" "+college);
}  
}    
public class TestStaticMethod{  
    public static void main(String args[]){  
    Student.change();        //calling change method  
    //creating objects  
    Student s1 = new Student(20201,"sachin");  
    Student s2 = new Student(20202,"amit");  
   
    //calling display method  
    s1.display();  
    s2.display();  
   
    }  
}  

OUTPUT : 
20201 sachin vwxyz 
20202 amit vwxyz

 


RECOMMENDED POST :