static keyword in JAVA-hindi-?-vishmy1.blogspot.com

static keyword in JAVA

The static keyword is used in JAVA mainly for memory management. it is used with variable, method, block & nested class. It is a keyword that is used to store the same variable or method of a given class.

JAVA में मुख्य रूप से memory management के लिए static कीवर्ड का उपयोग किया जाता है। यह variablemethod block और nested class के साथ प्रयोग किया जाता है। यह एक कीवर्ड है जो किसी दिए गए class के समान variable या method को store करने के लिए उपयोग किया जाता है।

Static keyword in JAVA-hindi-?-vishmy1.blogspot.com
static keyword is used with them

No object needs to be created to use a static variable or static method.

static variable या static method का उपयोग करने के लिए किसी object को बनाने की आवश्यकता नहीं है।

To create such a member, its declaration should be static keyword, and when a member is declared static. It can exist before any object of the class and without reference.  

ऐसा member बनाने के लिए, इसकी declaration  static keyword होनी चाहिए, और जब कोई  member, static घोषित किया जाता है। यह class के किसी भी ऑब्जेक्ट से पहले और reference के बिना मौजूद हो सकता है।

Method or variable declared as static have several restrictions.

  1.  They can only call other static methods.                                       
  2.  They must only exist static data.                                                   
  3.  They can't refer to this or super keyword to the static.
ex:-  

class test
{
    static int count = 0;
    test()
    {
         count++;
         System.out.println(count); 
    }
    public satatic void main(String args[])
    {
        test t = new test();
        test t1 = new test();
        test t2 = new test();
    }
}

When and why we use static keyword:-

Suppose we want to store all records of students of a college, the student Id of all the students are unique but there is college name of all the student are same at that time we use static keyword, so that we need only one memory space to be allocated for all the students, otherwise it allocates a memory space each time for all the students.

मान लें कि हम किसी कॉलेज के छात्रों के सभी रिकॉर्ड store करना चाहते हैं, सभी छात्रों की छात्र आईडी unique है, लेकिन सभी छात्रों के कॉलेज का नाम समान है, उस समय हम static कीवर्ड का उपयोग करते हैं, ताकि हमें केवल एक मेमोरी स्पेस की आवश्यकता हो सभी छात्रों के लिए आवंटित किया जाए, अन्यथा यह सभी छात्रों के लिए हर बार एक मेमोरी स्पेस आवंटित करता है।



        
final vs static keyword in JAVA


Static keyword in JAVA-hindi-?-vishmy1.blogspot.com
final vs static keyword

The final keyword always fixed the value of the variable, which means that it makes constant the value of a variable, constant, whereas static keyword always fixed the memory space for the same variable value, it means that it creates the memory space once for the same variable values. static will be located once in the program.

Note:-  Every final variable can be declared as static, but it is not compulsory that every static variable declared as final


In the next blog I'm going to explain. 
  1. static variable.
  2. static method.
  3. static block.
  4. nested class.

Note:- Please write a comment if you find anything incorrect. 

 If you like it please share it with your friends also so that they also read this and gain some knowledge.

Thank you.

Post a Comment

0 Comments