Clean β’ Professional
In Java, variables are containers used to store data values. Every variable has a data type, a name, and a value. They help programs store, update, and reuse information efficiently.
In this section of our Core Java Tutorial, youβll learn everything about Java variables β their types, scope, memory allocation, best practices, and even advanced concepts like shadowing and type inference.
A variable is a named memory location that holds a value which can change during program execution.
int age = 25;
String name = "John";
Here,
int β data typeage β variable name25 β valueDeclaration:
int number; // variable is declared but not initialized
Initialization:
number = 10; // value assigned separatelyCombined Declaration & Initialization:
int number = 10; // declare and initialize in a single line
Java variables are mainly categorized into three types based on their position and scope:

public class Example {
public void display() {
int number = 10; // local variable
System.out.println("Local Variable: " + number);
}
}
public class Student {
String name; // instance variable
int age; // instance variable
}
static keywordpublic class Counter {
static int count = 0; // static variable
Counter() {
count++;
}
}
public class Student {
static String schoolName = "ABC School"; // static variable
int rollNo; // instance variable
void setRollNo(int rollNo) {
int marks = 90; // local variable
this.rollNo = rollNo;
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
System.out.println("School: " + schoolName);
}
}
Use the final keyword to make a variable constant (value cannot change).
final double PI = 3.14159; // cannot be changed
If you try to reassign it:
PI = 3.14; // Error β cannot assign value to final variable| Data Type | Default Value |
|---|---|
| byte, short, int, long | 0 |
| float, double | 0.0 |
| char | '\u0000' |
| boolean | false |
| Objects / Arrays | null |
Note: Local variables donβt have default values β they must be initialized before use.
When a local variable has the same name as an instance variable, it hides (or shadows) the instance variable.
public class Test {
int value = 10; // instance variable
void display() {
int value = 20; // local variable shadows instance variable
System.out.println("Local Value: " + value); // 20
System.out.println("Instance Value: " + this.value); // 10
}
}
Use this.variableName to access the instance variable.
Variables can also be initialized at runtime, not just during declaration.
int a = 10;
int b = 20;
int sum = a + b; // dynamically initialized
var (Java 10+)Modern Java supports type inference using var β the compiler automatically infers the type.
var message = "Hello Java"; // inferred as String
var count = 10; // inferred as int
Rules for var:
| Type | Memory Area | Lifetime |
|---|---|---|
| Local Variables | Stack | Until method ends |
| Instance Variables | Heap | Until object is garbage collected |
| Static Variables | Method Area | Until class is unloaded |
public class ScopeExample {
static int staticVar = 100; // class-level
int instanceVar = 50; // object-level
void display() {
int localVar = 10; // method-level
System.out.println(localVar);
System.out.println(instanceVar);
System.out.println(staticVar);
}
}
Each variable has its own visibility and lifetime, depending on where it is declared.
You can declare multiple variables of the same data type in one line:
int x = 10, y = 20, z = 30;
System.out.println(x + ", " + y + ", " + z);
Use System.out.println() to display variable values.
int age = 25;
String name = "John";
double salary = 50000.50;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Salary: " + salary);
Output:
Name: John
Age: 25
Salary: 50000.5