आपकी ऑफलाइन सहायता

BACK
49

सी प्रोग्रामिंग

149

पाइथन प्रोग्रामिंग

49

सी प्लस प्लस

99

जावा प्रोग्रामिंग

149

जावास्क्रिप्ट

49

एंगुलर जे.एस.

69

पी.एच.पी.
माय एस.क्यू.एल.

99

एस.क्यू.एल.

Free

एच.टी.एम.एल.

99

सी.एस.एस.

149

आर प्रोग्रामिंग

39

जे.एस.पी.





डाउनलोड पी.डी.एफ. ई-बुक्स
Java - Variables

Variables ये memory locations के नाम होते है | जो values; variables को दी जाती हैं, वो उस location पर store हो जाती है |

Syntax for Variable Declaration

data_type_name variable_name; //or
data_type_name variable_name1, variable_name2;

For Example,

int a;
int b, c;

Syntax for Variable Definition

data_type variable_name = variable_value;

For Example,

int a = 5;
int b = 10, c = 15;

Java के लिए Variables के तीन प्रकार होते है |

  1. Local Variable
  2. Instance Variable
  3. Static Variable

1. Local Variable

Local Variables block, methods और constructor के अन्दर होते है |

Local Variable का scope; local होता है | ये सिर्फ methods और constructor के अन्दर visible होते है |

जब Local Variables; methods और constructor के बाहर जाते है, तब destroyed हो जाते है |

Source Code :
class Sample{
    
   void display(){
       int a = 5;   //Local Variable
       System.out.println("Value of a : " + a);
    }
    public static void main(String arg[]){
        Sample s = new Sample();
        s.display();
    }
}
Output :
Value of a : 5

2. Instance Variable

Instance Variables; class के अन्दर होते है और methods और constructor के बाहर होते है |

Instance Variables non-static variables होते है |

Source Code :
class Sample{
    int a = 5;  //Instance Variable
   void display(){
       System.out.println("Value of a : " + a);
    }
    public static void main(String arg[]){
        Sample s = new Sample();
        s.display();
    }
}
Output :
Value of a : 5

3. Static Variable

Static Variables को Class Variables भी कहते है |

ये Instance Variable के तरह class के अन्दर और methods और constructor के बाहर होते है |

'static' keyword के साथ इनका इस्तेमाल किया जाता है |

Source Code :
class Sample{
    static int a = 5;  //Static Variable
   void display(){
       System.out.println("Value of a : " + a);
    }
    public static void main(String arg[]){
        Sample s = new Sample();
        s.display();
    }
}
Output :
Value of a : 5