सीधे मुख्य सामग्री पर जाएं

Variables and Constants in Java – Complete Guide with Examples, Types, Scope & Interview Questions (2026)

Variables and Constants in Java – Complete Guide

Variables and Constants in Java – Complete Master Guide

Variables and Constants in Java

Java programming me variable ek container hota hai jo data store karta hai. Har program me data ko temporarily ya permanently store karne ke liye variables ka use hota hai. Agar aap Java seekh rahe hain to variables sabse important concept hai.


What is a Variable?

Variable ek named memory location hoti hai jisme value store hoti hai. Jab program run hota hai tab memory allocate hoti hai.

int age = 25;

Yaha: • int = data type • age = variable name • 25 = value


Rules for Declaring Variables

  • Variable name number se start nahi ho sakta
  • Special characters allowed nahi
  • Java keywords use nahi kar sakte
  • Case sensitive hote hain

Types of Variables in Java

1️⃣ Local Variable

Method ke andar declare hota hai.

void display(){ int num = 10; }

Local variable sirf method ke andar accessible hota hai.

2️⃣ Instance Variable

Class ke andar but method ke bahar declare hota hai.

class Student{ String name; }

Object create hone par instance variable initialize hota hai.

3️⃣ Static Variable

Static keyword ke sath declare hota hai.

class Test{ static int count = 0; }

Static variable class level par shared hota hai.


Memory Concept (Stack vs Heap)

Stack Memory Heap Memory
Local variables Objects
Fast access Dynamic memory

What is a Constant?

Constant wo variable hota hai jiska value change nahi hota. Java me final keyword se constant banate hain.

final double PI = 3.14159;

Why Use Constants?

  • Security
  • Fixed configuration values
  • Mathematical constants
  • Better readability

Naming Convention

Variable: camelCase Constant: UPPER_CASE

int studentAge; final double MAX_VALUE = 100;

Default Values of Variables

Data Type Default Value
int 0
boolean false
Object null

Interview Questions

Q1: Difference between instance and static variable?

Instance object specific hota hai, static class specific hota hai.

Q2: What happens if variable not initialized?

Local variable error dega.

Q3: Why use final keyword?

To make variable constant.


Common Mistakes

  • Uninitialized local variable
  • Wrong naming convention
  • Static misuse

Best Practices

  • Meaningful variable names
  • Avoid global variables
  • Use constants for fixed values
  • Follow Java naming standards

Conclusion

Variables aur constants Java programming ka foundation hain. Inhe samajhna bahut zaruri hai agar aap software developer banna chahte hain.

Related Posts: Non-Primitive Data Types | Type Casting in Java

टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट

Jdk-jre-jvm-explained-in-hindi

JDK JRE JVM Explained in Hindi – Java Architecture Complete Guide ☕ JDK, JRE, JVM Explained in Hindi (Complete Guide) AdSense Ad 🔰 Java Overview Java ek powerful programming language hai jo duniya bhar me use hoti hai... Write Once Run Anywhere (WORA) Java ka sabse bada benefit ye hai ki ye platform independent hai... Java Features Object Oriented Secure Robust Multithreaded Platform Independent 🔰 Java Overview (Detailed) Java ek powerful, high-level aur object-oriented programming language hai jo 1995 me Sun Microsystems (ab Oracle) dwara develop ki gayi thi. Java ka main purpose tha ek aisi language banana jo simple ho, secure ho aur sabhi platforms par run kar sake. Java ko duniya bhar me millions of developers use karte hain web applications, mobile apps (Android), desktop software, enterprise systems aur even cloud-based applications banane ke liye. Java ki sabse badi khasiyat hai iska slogan: Write Once, Run Anywhere (WORA) ...

Java Kya Hai? History, Features Aur Use – Complete Java Guide in Hindi

Day 1 Day 2 Day 3 Day 4 Day 5 Day 6 Day 7 Java Programming Language – History, Features aur Uses Java Kya Hai? History, Features Aur Use – Complete Java Guide in Hindi Java ek powerful aur popular programming language hai jo duniya bhar me software, web application aur mobile app development ke liye use hoti hai. Is article me hum detail me samjhenge ki Java kya hai , iska history kya hai aur Java ke main features kaun-kaun se hain. Java Programming Language Kya Hai? Java ek object-oriented , platform-independent programming language hai jise Sun Microsystems ne develop kiya tha. Java ka main concept hai “Write Once, Run Anywhere” , matlab ek baar likha gaya Java code kisi bhi operating system par run ho sakta hai. Java Ka Itihas (History of Java) Java language ka development 1991 me Sun Microsystems ke engineer James Gosling aur unki team ne shuru kiya tha. Shuru me Java ka naam Oak tha, jo baad me change karke Java rakha gaya. 199...

RBI Office Attendant Result 2026 Kab Aayega? Expected Cut Off & Passing Marks Details

RBI Office Attendant Result 2026 Kab Aayega? Expected Cut Off & Passing Marks RBI Office Attendant Result 2026 Kab Aayega? Expected Cut Off & Passing Marks RBI Office Attendant Exam 2026 28 February aur 1 March 2026 ko successfully conduct ho chuka hai. Ab sabhi candidates ka sabse bada sawal hai – RBI Office Attendant Result 2026 kab aayega? Is article me aapko expected result date, passing marks, expected cut off aur result ke baad kya karna hai sab detail me bataya gaya hai. 📢 Latest Update: RBI result usually exam ke 4–6 weeks ke andar release hota hai. 📅 RBI Office Attendant Result 2026 Expected Date Previous trends ke hisaab se RBI written exam ke 30–45 din ke andar result declare karta hai. Exam Date 28 Feb & 1 March 2026 Expected Result Date April 2026 (Mid ya End tak) Official Website opportunities.rbi.org.in 📊 RBI Office Attendant Passing Marks Kitne Hain? RBI fixed passing percentage announce nahi karta. Selection cut ...