⏱️ 6 min read • Beginner Level • Lesson 10
Completed on . You can revise this lesson or continue to the next topic.
Variables in Java are named memory locations used to store data. They allow programs to save values and change them during execution. Understanding variables is essential before learning data types, operators, and control statements.
Variables are named storage locations in memory used to hold values.
Every variable has a data type that decides the kind of values it can store (int, float, String, etc.).
They can take different values at different times during execution.
A variable must be initialized before using it, otherwise the compiler will throw an error.
DataType identifier = value;
A local variable is declared inside a method or block and its scope is limited to that block.
They don’t get default values and must be initialized before use.
A non-static variable declared inside a class but outside methods is called an instance variable. Each object gets its own copy of instance variables.
They are automatically initialized with default values (e.g., 0 for int, null for objects).
A static variable is declared with the static keyword inside a class.
It belongs to the class rather than any object.
Static variables cannot be local.
Since only one copy exists per class, changing it affects all objects.
public class Test {
int x = 50; // instance variable
static int y = 100; // static variable
void myMethod() {
int z = 150; // local variable
}
}
x is an instance variable.y is a static variable.z is a local variable.Here: x is tied to an object, y is tied to the class, and z exists only during method execution.
Local variables must be initialized before use, while instance and static variables get default values.
In Java, instance and class (static) variables are automatically initialized with default values if not explicitly assigned. Local variables do not have default values and must be initialized before use.
| Data Type | Default Value |
|---|---|
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| float | 0.0f |
| double | 0.0d |
| char | '\u0000' (null character) |
| boolean | false |
| Objects (e.g., String) | null |
This table applies to both instance and class (static) variables. Local variables must be explicitly initialized, or the compiler will throw an error.
🧠 Test your understanding with a quick quiz
Topic: Variables | Language: Java
public class Counter {
static int count = 0;
void increment() {
count++;
}
}public class Test {
int x; // instance variable
static int y; // static variable
public static void main(String[] args) {
Test obj = new Test();
System.out.println(obj.x);
System.out.println(y);
}
}public class Test {
int num = 10; // instance variable
public static void main(String[] args) {
Test t1 = new Test();
Test t2 = new Test();
t1.num = 20;
System.out.println(t1.num + " " + t2.num);
}
}public class Example {
public static void main(String[] args) {
int a; // local variable
System.out.println(a);
}
}🎉 Great job! Continue learning Java step by step.
Mark this lesson as completed to track your learning progress. To keep track across devices, please login and save your learning progress.
Discussion
Ask questions, share suggestions, or discuss this lesson.