Variables in Java
- एक Variable एक Memory location को दिया गया नाम है। यह एक Program में Storage की मूल इकाई है।
- Program के execution के दौरान एक Variable में Stored value को बदला जा सकता है।
- एक Variable केवल एक Memory location को दिया गया नाम है, Variable पर किए गए सभी Operation Memory location को effect करते हैं।
- Java में, उपयोग से पहले सभी Variable declare किए जाने चाहिए।
How to declare variables?
Image से, यह आसानी से माना जा सकता है कि एक Variable declare करते समय हमें दो चीजों का ध्यान रखने की आवश्यकता होती है जो कि करते हैं
Datatype: datatype जिसे इस Variable में store किया जा सकता है.
Data name: Variable को दिया गया name.
इस तरह, एक name केवल Memory location को दिया जा सकता है। इसे दो तरह से Value दिया जा सकता है:
- Variable Initialization
- Assigning value by taking input
How to initialize variables?
इसे 3 Components की सहायता से Consider किया जा सकता है जो इस प्रकार हैं:
- datatype: datatype जिसे इस Variable में Store किया जा सकता है.
- variable_name: Variable को दिया गया name.
- value: यह Variable में Stored initial value है.
Illustrations:
float simpleInterest; // Declaring float variable
int time = 10, speed = 20; // Declaring and Initializing integer variable
char var = 'h'; // Declaring and Initializing character variable
आइए अब हम विभिन्न प्रकार के चरों पर चर्चा करें जो इस प्रकार सूचीबद्ध हैं:
- Local Variables
- Instance Variables
- Static Variables
आइए हम प्रत्येक Variable के Characters पर details में discuss करें
1. Local Variables
किसी block या method या Constructor के भीतर defined variables को Local Variable कहा जाता है।
- ये Variable तब बनाए जाते हैं जब block enter किया जाता है या block से बाहर निकलने के बाद Function को Call और destroy कर दिया जाता है या जब Function से Call return आती है।
- इन Variables का Scope केवल उस block के भीतर मौजूद होता है जिसमें Variable declare किया जाता है। यानी हम इन Variables को केवल उस block के भीतर ही access कर सकते हैं।
- defined scope में उपयोग करने से पहले local variables का प्रारंभ अनिवार्य है।
2. Instance Variables
- Instance Variables non-static variables हैं और किसी भी Method, Constructor, ……………………………………………….. या Block के बाहर एक Class में declare किए जाते हैं।
- जैसा कि एक Class में Instance Variable declare किए जाते हैं, ये Variable तब बनाए जाते हैं जब Class की कोई Object बनाई जाती है और जब object destroy हो जाती है तो destroy हो जाती है।
- Unlike local variables, हम Instance Variable के लिए access specifier का उपयोग कर सकते हैं। यदि हम कोई access specifier specify नहीं करते हैं तो default access specifier का उपयोग किया जाएगा।
- Instance Variable का Initialization mandatory नहीं है। इसका default value 0 . है
- Instance Variable को केवल Object बनाकर ही Access किया जा सकता है।
3. Static Variables
Static Variables को Class Variables के रूप में भी जाना जाता है।
- इन Variables को Instance Variables के समान declare किया जाता है, difference यह है कि Static Variables को किसी भी Method constructor या block के बाहर Class के under Static Keyword का उपयोग करके declare किया जाता है।
- Instance Variable के विपरीत, हमारे पास प्रति Class एक static variable की केवल एक Copy हो सकती है, भले ही हम कितनी भी objects बनाएँ।
- Program के execution की Start में static variable बनाए जाते हैं और execution ends होने पर automatically destroy हो जाते हैं।
- Static Variable का Initialization mandatory नहीं है। इसका default value 0 . है
- यदि हम Static Variables जैसे Instance Variables (Object के माध्यम से) का उपयोग करते हैं, तो Compiler Warning message दिखाएगा और यह Program को नहीं रोकेगा। Compiler Object name को Class के name से automatically बदल देगा।
- यदि हम Class के name के बिना Static Variable का उपयोग करते हैं, तो Compiler automatically Class name जोड़ देगा।
Differences between the Instance variable Vs. the Static variables
- प्रत्येक Object के पास Instance Variable की own copy होगी जबकि हमारे पास प्रति Class एक static variable की केवल एक copy हो सकती है, भले ही हम कितनी भी objects बनाएँ।
- एक object का उपयोग करके एक instance variable में किए गए change अन्य objects में reflected नहीं होंगे क्योंकि प्रत्येक object की instance variable की अपनी Copy होती है। static के Case में, changes अन्य objects में दिखाई देंगे क्योंकि static variables एक Class की all objects के लिए common हैं.
- हम object references के through instance variables तक पहुंच सकते हैं और Static Variables को direct Class name का उपयोग करके access किया जा सकता है।
Syntax: Static and instance variables
class DW { // Static variable static int a; // Instance variable int b; }