इस आर्टिकल में हम जावा static keyword क्या हैं | What is static keyword in Java in Hindi के बारे में जानेंगे।
What is static keyword in Java in Hindi?
जावा में static keyword का उपयोग मुख्य रूप से memory management के लिए किया जाता है। हम static keyword को variables, methods, blocks और nested classes के साथ apply कर सकते हैं। static keyword class के एक instance की तुलना में class से संबंधित है।
static हो सकता है:
- Variable (class variable के रूप में भी जाना जाता है)
- Method (एक class method के रूप में भी जाना जाता है)Block
- Block
- Nested class
1) Java static variable in Hindi
यदि आप किसी variable को static declare करते हैं, तो इसे static variable के रूप में जाना जाता है।
- static variable का उपयोग सभी objects की common property (जो प्रत्येक object के लिए unique नहीं है) को संदर्भित करने के लिए किया जा सकता है, उदाहरण के लिए, employees की कंपनी का नाम, छात्रों के कॉलेज का नाम आदि।
- class लोडिंग के समय static variable को क्लास area में केवल एक बार मेमोरी मिलती है।
Constructors क्या हैं? | What is Constructors in Java in Hindi
Java में Method क्या हैं?| What is Method in Java in Hindi – 2023
Advantages of static variable
यह आपके program मेमोरी को efficient बनाता है (यानी, यह memory saves करता है)।
Understanding the problem without static variable
class Student{
int rollno;
String name;
String college="ITS";
}
मान लीजिए कि मेरे कॉलेज में 500 students हैं, अब ऑब्जेक्ट बनने पर सभी इंस्टेंस डेटा सदस्यों को हर बार मेमोरी मिल जाएगी। सभी छात्रों का अपना unique rollno और नाम होता है, इसलिए ऐसे मामले में इंस्टेंस डेटा member अच्छा होता है। यहाँ, “college” सभी objects की common property को refers करता है। यदि हम इसे static बनाते हैं, तो इस फील्ड को केवल एक बार मेमोरी मिलेगी।
जावा static प्रॉपर्टी को सभी objects में शेयर किया जाता है।
Example of static variable
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
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(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
}
}
Output:
111 Karan ITS
222 Aryan ITS
Program of the counter without static variable in Hindi
इस उदाहरण में, हमने एक instance variable बनाया है जिसका नाम count है जो कि constructor में है। चूंकि इंस्टेंस वेरिएबल को ऑब्जेक्ट बनाते समय मेमोरी मिलती है, इसलिए प्रत्येक ऑब्जेक्ट में इंस्टेंस वेरिएबल की कॉपी होगी। अगर इसे बढ़ाया जाता है, तो यह अन्य वस्तुओं को reflect नहीं करेगा। इसलिए प्रत्येक object का count variable में value 1 होगा।
//Java Program to demonstrate the use of an instance variable
//which get memory each time when we create an object of the class.
class Counter{
int count=0;//will get memory each time when the instance is created
Counter(){
count++;//incrementing value
System.out.println(count);
}
public static void main(String args[]){
//Creating objects
Counter c1=new Counter();
Counter c2=new Counter();
Counter c3=new Counter();
}
}
Output:
1
1
1
2) Java static method in Hindi
यदि आप किसी भी method के साथ static keyword apply करते हैं, तो इसे static method के रूप में जाना जाता है।
- एक static method class की object के बजाय class से संबंधित होती है।
- एक class का instance बनाने की आवश्यकता के बिना एक static method को लागू किया जा सकता है।
- एक static method डेटा सदस्य तक पहुँच सकता है और इसका मान बदल सकता है।
Example of static method
//Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
}
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){System.out.println(rollno+" "+name+" "+college);}
}
//Test class to create and display the values of object
public class TestStaticMethod{
public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method
s1.display();
s2.display();
s3.display();
}
}
Output
111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
Restrictions for the static method in Hindi
- static method non-static डेटा member का उपयोग नहीं कर सकता है या non-static method को सीधे कॉल कर सकती है।
- यह और super static context में उपयोग नहीं किया जा सकता है।
3) Java static block in Hindi
- static डेटा member को initialize करने के लिए उपयोग किया जाता है।
- classloading के समय इसे main method से पहले executed किया जाता है। Read More
Example of static block
class A2{
static{System.out.println("static block is invoked");}
public static void main(String args[]){
System.out.println("Hello main");
}
}
Output
static block is invoked
Hello main
- Objects and Classes in Java in Hindi
- Java Naming Convention in Hindi
- JavaScript क्या है? | what is javaScript in hindi? | advantage javaScript in hindi
- what is loops statement in java in Hindi |जावा लूप क्या हैं
आशा करता हूँ कि आज के इस आर्टिकल में आपको static keyword क्या हैं | What is static keyword in Java in Hindi से सम्बंधित आपके सारे आंसर मिल गए होंगे।
आज के इस आर्टिकल में हमने जाना कि static keyword क्या हैं | What is static keyword in Java in Hindi इस आर्टिकल से संबधित आपका कोई प्रश्न हैं तो आप comment करें में आपकी कमेंट का जबाब जरूर दूंगा।
ऐसे ही IT, Computer Science से रिलेटेड जानकारियाँ पाने के लिए हमारे इस वेबसाइट को सब्सक्राइब कर लीजिये | जिससे आपको हमारी आने वाले नये आर्किल की notification प्राप्त हो सके।
अगर जानकारी अच्छी लगी हो तो अपने दोस्तों और class mate के साथ ये जानकारी जरूर शेयर करें जिससे उनकी भी हेल्प हो सके।
FAQ प्रश्न-
-
What is static keyword in Java in Hindi?
जावा में static keyword का उपयोग मुख्य रूप से memory management के लिए किया जाता है। हम static keyword को variables, methods, blocks और nested classes के साथ apply कर सकते हैं। static keyword class के एक instance की तुलना में class से संबंधित है।