1. Introduction
A variable in Java is a named memory location that holds a value which can be manipulated during program execution. Every variable in Java has a data type that determines the size and layout of the variable's memory, the range of values that can be stored, and the set of operations that can be applied to it.
Cricket analogy: A variable is like a labeled locker in the dressing room holding a player's gear; its type (batting kit vs bowling kit) determines what fits inside and what operations (bowling vs batting) can be done with it.
Java is a statically and strongly typed language, which means every variable must be declared with a specific type before it is used, and that type cannot change at runtime. This helps the compiler catch type-related errors early, before the program even runs.
Cricket analogy: Java is like a strict team-sheet rule where a player's role (bowler or batsman) is declared before the match and can't switch mid-innings; the team management (compiler) catches a miscategorized player before the match even starts.
2. Syntax
dataType variableName; // declaration
dataType variableName = value; // declaration with initialization
int age; // declared, not initialized
int marks = 90; // declared and initialized
String name = "Alice"; // reference type variable3. Explanation
Java supports three main kinds of variables. A local variable is declared inside a method, constructor, or block and exists only for the duration of that block; it must be initialized before use because it has no default value. An instance variable is declared inside a class but outside any method, and each object of the class gets its own copy; it is automatically initialized to a default value (0, null, false, etc.). A static variable is declared with the static keyword and belongs to the class itself rather than any individual object, so it is shared across all instances.
Cricket analogy: A local variable is like chalk marks a fielder makes for a single over that vanish after it; an instance variable is like each player's personal kit that's theirs alone; a static variable is like the team's single shared trophy cabinet, common to everyone on the squad.
Variable names in Java must begin with a letter, underscore (_), or dollar sign ($), followed by letters, digits, underscores, or dollar signs. They are case-sensitive and cannot be a reserved keyword such as class or int. By convention, Java uses camelCase for variable names, e.g. totalMarks.
Cricket analogy: Naming a scoring variable is like naming a player — it must start with a proper letter (not a number like "1Kohli"), is case-sensitive (Sachin vs sachin differ), can't be a reserved term like "out," and convention favors camelCase like totalRuns.
Exam trap: A local variable in Java does NOT get a default value. Using an uninitialized local variable causes a compile-time error ('variable might not have been initialized'), unlike instance and static variables, which are auto-initialized (0/0.0/false/null).
4. Example
public class VariableDemo {
static int staticVar = 100; // static variable
int instanceVar = 10; // instance variable
void show() {
int localVar = 5; // local variable, must be initialized
System.out.println("Static: " + staticVar);
System.out.println("Instance: " + instanceVar);
System.out.println("Local: " + localVar);
}
public static void main(String[] args) {
VariableDemo obj = new VariableDemo();
obj.show();
}
}5. Output
Static: 100
Instance: 10
Local: 56. Key Takeaways
- A variable is a named memory location with a fixed data type.
- Java has three variable kinds: local, instance, and static.
- Local variables must be explicitly initialized before use; they have no default value.
- Instance and static variables get automatic default values.
- Static variables are shared across all objects of a class.
- Variable names are case-sensitive and cannot be reserved keywords.
Practice what you learned
1. Which type of variable does NOT get a default value in Java?
2. Which keyword makes a variable belong to the class rather than to individual objects?
3. What is the default value of an uninitialized instance variable of type boolean?
4. Which of the following is a valid Java variable name?
5. In Java, variables must be declared with a specific data type. This makes Java a:
Was this page helpful?
You May Also Like
Data Types in Java
A complete guide to Java's 8 primitive data types with exact sizes and ranges, plus reference types like String and arrays.
Constants (final keyword) in Java
Learn how to declare constants in Java using the final keyword, and understand its use with variables, methods, and classes.
Operators in Java
Explore Java's arithmetic, relational, logical, bitwise, assignment, unary, ternary, and instanceof operators along with operator precedence.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics