🟢 Foundation · Lesson 05
Variables in Java
Variables in Java
What is a Variable?
A variable is a named container that stores a value in memory. In Java every variable must have a type.
int age = 18;
String name = "Aman";
double price = 99.5;
Types of Variables
| Type | Where Declared |
|---|---|
| Local | inside a method, used only there |
| Instance | inside a class, one per object |
| Static | shared by all objects (uses static) |
Example
public class Demo {
static int count = 0; // static
int id; // instance
void show() {
int local = 5; // local
System.out.println(local);
}
}
Summary
- A variable stores a typed value.
- Local (in method), instance (per object), static (shared).
Variable क्या है?
Variable एक named container है जो memory में value रखता है। Java में हर variable का एक type होना ज़रूरी है।
int age = 18;
String name = "Aman";
double price = 99.5;
Variables के प्रकार
| Type | कहाँ declare |
|---|---|
| Local | method के अंदर, सिर्फ वहीं |
| Instance | class के अंदर, हर object का एक |
| Static | सभी objects में साझा (static) |
Example
public class Demo {
static int count = 0; // static
int id; // instance
void show() {
int local = 5; // local
System.out.println(local);
}
}
सारांश
- Variable typed value रखता है।
- Local (method में), instance (हर object), static (साझा)।