Java Variables
A variable in Java is a container used to store data values.
Variables allow a program to store, modify, and use information during execution.
What Is a Variable?
A variable is simply a named memory location.
Example:
int age = 25;
Here:
int→ data typeage→ variable name25→ value stored in the variable
Why Variables Are Important
Variables help to:
- Store data
- Perform calculations
- Manage program logic
- Make programs dynamic
Without variables, programs cannot process information effectively.
Syntax of Variable Declaration
Basic Syntax
dataType variableName = value;
Example:
int number = 10;
Types of Variables in Java
Java mainly has 3 types of variables:
- Local Variables
- Instance Variables
- Static Variables
1. Local Variables
A local variable is declared inside a method, constructor, or block.
Example
class Demo {
void show() {
int x = 100;
System.out.println(x);
}
}
Characteristics
- Created when method starts
- Destroyed when method ends
- Accessible only inside that method
2. Instance Variables
Instance variables are declared inside a class but outside methods.
Each object gets its own copy.
Example
class Student {
String name;
int age;
}
Characteristics
- Belong to objects
- Different objects can have different values
- Stored in heap memory
3. Static Variables
Static variables belong to the class, not objects.
Only one copy exists.
Example
class College {
static String collegeName = "ABC College";
}
Characteristics
- Shared among all objects
- Memory efficient
- Accessed using class name
Example of All Variable Types
class Employee {
static String company = "Tech Ltd"; // Static variable
String name; // Instance variable
void display() {
int salary = 50000; // Local variable
System.out.println(name);
System.out.println(company);
System.out.println(salary);
}
}
Java Data Types Used in Variables
Variables store data according to their data types.
Primitive Data Types
| Data Type | Size | Example |
|---|---|---|
| byte | 1 byte | byte b = 10; |
| short | 2 bytes | short s = 100; |
| int | 4 bytes | int x = 50; |
| long | 8 bytes | long l = 1000L; |
| float | 4 bytes | float f = 5.5f; |
| double | 8 bytes | double d = 99.99; |
| char | 2 bytes | char c = 'A'; |
| boolean | 1 bit | boolean flag = true; |
Examples of Variables
Integer Variable
int marks = 95;
Decimal Variable
double price = 199.99;
Character Variable
char grade = 'A';
Boolean Variable
boolean isJavaFun = true;
Rules for Naming Variables
Allowed
✅ Letters
✅ Digits
✅ _ underscore
✅ $ dollar sign
Rules
- Must start with a letter,
_, or$ - Cannot start with a number
- Cannot use Java keywords
- Variable names are case-sensitive
Valid Variable Names
int age;
double salary;
String studentName;
Invalid Variable Names
int 1age; // Invalid
int class; // Keyword
int student-name; // Invalid symbol
Java Naming Conventions
Java follows camelCase naming style.
Good Practice
int studentAge;
double accountBalance;
Variable Initialization
Assigning value to a variable is called initialization.
Example
int number = 20;
Declaration vs Initialization
Declaration
int age;
Initialization
age = 25;
Reassigning Variables
Variable values can change.
int score = 10;
score = 20;
Constants in Java
If a value should never change, use final.
final double PI = 3.14159;
Now PI cannot be modified.
Variable Scope
Scope means where a variable can be accessed.
Local Scope
void test() {
int x = 10;
}
x exists only inside test().
Class Scope
class Demo {
int y = 20;
}
y can be used by all methods in the class.
Default Values of Variables
Instance and static variables get default values automatically.
| Type | Default Value |
|---|---|
| int | 0 |
| double | 0.0 |
| boolean | false |
| char | ‘\u0000’ |
| Object | null |
Local variables do NOT get default values.
Example Program
public class VariablesDemo {
static String company = "OpenAI";
String employeeName = "Rahul";
public static void main(String[] args) {
int age = 25;
VariablesDemo obj = new VariablesDemo();
System.out.println("Company: " + company);
System.out.println("Employee: " + obj.employeeName);
System.out.println("Age: " + age);
}
}
Output
Company: OpenAI
Employee: Rahul
Age: 25
Common Mistakes Beginners Make
1. Using Uninitialized Local Variables
int x;
System.out.println(x); // Error
2. Confusing Static and Instance Variables
Static variables belong to class.
Instance variables belong to objects.
3. Wrong Naming
int Student Age; // Invalid