Java Variables – Declare, Initialize & Use Variables in Java
Introduction – What Are Variables in Java?
Every Java program revolves around data — and variables are how we store and manipulate that data. Think of a variable as a container with a label, used to hold different kinds of information like numbers, text, or boolean values.
Whether you’re calculating tax or storing user input, understanding Java variables is a fundamental step.
By the end of this guide, you’ll be able to:
- Declare and initialize variables
- Understand Java’s variable types and naming rules
- Use variables in expressions and output
- Follow best practices for clean, readable code
What Is a Variable in Java?
A variable in Java is a named memory location that stores a value.
Syntax:
type variableName = value;
Example:
int age = 25;
Explanation:
int: data type (integer)age: variable name25: value assigned to the variable
Types of Variables in Java
Java supports several types of variables, depending on where and how they are declared.
| Type | Scope | Defined Inside | Example |
|---|---|---|---|
| Local | Method | Method or block | int count = 10; |
| Instance | Object | Class (outside any method, without static) | String name; |
| Static | Class | Class with static keyword | static int totalUsers; |
Tip: Always initialize local variables before using them.
Declaring and Initializing Variables
Declare First, Assign Later:
int score;
score = 100;
Declare and Assign Together:
String city = "Mumbai";
Multiple Variables of Same Type:
int x = 10, y = 20, z = 30;
Warning: Avoid using undeclared or uninitialized variables — it causes a compile-time error.
Variable Naming Rules
Allowed:
- Letters (
a–z,A–Z) - Digits (
0–9) - Underscore (
_) and Dollar sign ($)
Not Allowed:
- Names starting with a digit
- Reserved keywords (
class,int, etc.)
Examples:
| Valid Names | Invalid Names |
|---|---|
userName | 1user (starts with digit) |
total_amount | int (Java keyword) |
MAX_SIZE | @value (invalid symbol) |
Best Practice: Use camelCase for variables (firstName, totalAmount).
Example: Using Variables in Java
public class UserProfile {
public static void main(String[] args) {
String name = "Alice";
int age = 30;
double height = 5.7;
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("Height: " + height + " feet");
}
}
Output:
Name: Alice
Age: 30
Height: 5.7 feet
Explanation:
- Different data types are used (
String,int,double) - All values are printed using
System.out.println()
Java Variable Data Types (Quick Overview)
| Type | Description | Example Value |
|---|---|---|
int | Integer numbers | 100, -45 |
double | Decimal numbers | 3.14, -9.8 |
char | Single character | 'A', '9' |
boolean | True/false value | true, false |
String | Sequence of characters | "Hello" |
You’ll explore these in detail in the Java Data Types section.
Summary – Java Variables Essentials
Java variables are the foundation of storing and manipulating data. You’ve learned:
- How to declare and initialize variables
- Types of variables: local, instance, static
- Naming rules and best practices
- How to use variables in Java programs.
FAQs – Java Variables
What is a variable in Java?
A variable is a container that holds a value — like a label on a storage box in memory.
Can a Java variable name start with a number?
No. Variable names cannot begin with digits, but digits can be used after the first character.
What happens if I don’t initialize a variable?
- Local variables must be initialized before use — or you’ll get a compile error.
- Instance variables get default values (
0,false,null, etc.).
What’s the difference between String and char?
char: stores a single character ('A')String: stores a sequence of characters ("Apple")
Share Now :
