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 के तीन प्रकार होते है |
- Local Variable
- Instance Variable
- Static Variable
1. Local Variable
Local Variables block, methods और constructor के अन्दर होते है |
Local Variable का scope; local होता है | ये सिर्फ methods और constructor के अन्दर visible होते है |
जब Local Variables; methods और constructor के बाहर जाते है, तब destroyed हो जाते है |
Source Code :Output :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(); } }
Value of a : 5
2. Instance Variable
Instance Variables; class के अन्दर होते है और methods और constructor के बाहर होते है |
Instance Variables non-static variables होते है |
Source Code :Output :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(); } }
Value of a : 5
3. Static Variable
Static Variables को Class Variables भी कहते है |
ये Instance Variable के तरह class के अन्दर और methods और constructor के बाहर होते है |
'static' keyword के साथ इनका इस्तेमाल किया जाता है |
Source Code :Output :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(); } }
Value of a : 5