Variables in Java
In this lesson: Learn Java variables — local, instance, static — declaration rules and naming with examples and output. In Hindi & English.
What is a variable
A variable is a named box in memory that holds a value. In Java, every variable has a fixed type that you state when you declare it — Java is statically typed. This means you always know what kind of value a variable can hold, which makes programs safer.
int age = 18; — the type int is required.Declaring variables
int age = 18; // whole number double price = 99.5; // decimal number char grade = 'A'; // single character boolean pass = true; // true / false String name = "Aman"; // text System.out.println(name + " is " + age);
The pattern is: type, name, =, value, then ;. Unlike loosely typed languages, you must always write the type in Java.
Common types
| Type | Holds | Example |
|---|---|---|
| int | Whole numbers | 18 |
| double | Decimal numbers | 99.5 |
| char | One character | 'A' |
| boolean | true / false | true |
| String | Text | "Aman" |
int, double, char and boolean are primitive types. String is a class for text, written with a capital S.
Naming rules
- Start with a letter, underscore or dollar sign — not a number.
- After that, letters, numbers and underscores are allowed.
- No spaces and no Java keywords (like
intorclass). - Case-sensitive:
ageandAgeare different. - By convention, use camelCase:
totalMarks, nottotal_marks.
Local vs instance variables
public class Student {
String name; // instance variable (per object)
void show() {
int marks = 90; // local variable (inside method)
System.out.println(name + " " + marks);
}
}
An instance variable (like name) belongs to each object and lasts as long as it. A local variable (like marks) exists only inside its method and disappears when the method ends.
Constants with final
final int MAX_USERS = 100; // MAX_USERS = 200; // ERROR: cannot change a final
Use final to make a variable a constant that cannot change after it is set. By convention, constant names are written in UPPERCASE, like MAX_USERS.
Common mistakes
- Forgetting the type. Java needs it:
int age = 18;, notage = 18;. - Wrong type for the value. Putting 99.5 into an
intloses the decimals or errors. - Case slips.
ageandAgeare different variables. - Using a variable before assigning it. Local variables must be given a value before use.
Summary
- A variable stores a value and, in Java, has a fixed type.
- Declare with type, name, value:
int age = 18;. - Common types: int, double, char, boolean, String.
- Local variables live in methods; instance variables belong to objects.
- Use
finalfor constants that cannot change.
Next, explore all types in Java data types, or work with numbers using Java operators.
Frequently Asked Questions
How do I declare a variable in Java?
int age = 18;. Unlike some languages, Java is statically typed, so you must state the type when you declare a variable.Why must I declare the type in Java?
int x can only hold whole numbers.What is the difference between int and double?
int stores whole numbers like 18, while double stores decimal numbers like 99.5. Use int for counts and IDs, and double for prices, averages and measurements.What is the difference between a local and an instance variable?
How do I make a constant in Java?
final keyword: final int MAX = 100;. Once set, a final variable cannot be changed. Constants are usually written in uppercase by convention.