Variables in Java
Introduction
A variable is a named memory location used to store data in a Java program. The value stored in a variable can be changed during program execution.
For example:
int age = 20;
Here:
int→ Data typeage→ Variable name20→ Value
Syntax of a Variable
dataType variableName = value;
Example:
int age = 20; String name = "Anuradha"; double salary = 25000.50;
Why Are Variables Used?
Variables are used to:
- Store data
- Reuse data in a program
- Perform calculations
- Store user input
- Make programs easier to understand and maintain
Types of Variables in Java
Java has three main types of variables:
- Local Variable
- Instance Variable
- Static Variable
1. Local Variable
A variable declared inside a method, constructor, or block is called a local variable.
Example:
public class Main { public static void main(String[] args) { int age = 20; System.out.println(age); } }
Here, age is a local variable because it is declared inside the main() method.
Characteristics
- Declared inside a method, constructor, or block.
- Can only be accessed within that method or block.
- Must be initialized before it is used.
2. Instance Variable
A variable declared inside a class but outside methods, constructors, or blocks is called an instance variable.
Example:
class Student { String name; int age; void display() { System.out.println(name); System.out.println(age); } }
Here, name and age are instance variables.
They belong to an object of the Student class.
Using Instance Variables
public class Main { public static void main(String[] args) { Student s1 = new Student(); s1.name = "Anuradha"; s1.age = 20; s1.display(); } }
3. Static Variable
A variable declared using the static keyword is called a static variable or class variable.
Example:
class Student { String name; static String college = "Rajarshi Janak University"; }
The static variable college is shared by all objects of the class.
Example
class Student { String name; static String college = "Rajarshi Janak University"; void display() { System.out.println(name); System.out.println(college); } } public class Main { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student(); s1.name = "Anuradha"; s2.name = "Ram"; s1.display(); s2.display(); } }
Data Types of Variables
Variables must have a data type.
Primitive Data Types
Java has eight primitive data types:
| Data Type | Example |
|---|---|
byte | byte age = 20; |
short | short number = 1000; |
int | int age = 20; |
long | long population = 100000L; |
float | float price = 10.5f; |
double | double salary = 25000.50; |
char | char grade = 'A'; |
boolean | boolean passed = true; |
Non-Primitive Variables
Examples include:
String- Arrays
- Classes
- Objects
- Interfaces
Example:
String name = "Anuradha";
Declaring a Variable
A variable can be declared without assigning a value.
int age;
The value can be assigned later:
age = 20;
Initializing a Variable
Initializing means assigning a value to a variable.
int age = 20;
Here, age is declared and initialized at the same time.
Changing a Variable's Value
The value of a variable can be changed.
int age = 20; age = 21; System.out.println(age);
Output:
21
Multiple Variables
Multiple variables of the same type can be declared together.
int x = 10, y = 20, z = 30;
However, declaring variables separately is often easier to read:
int x = 10; int y = 20; int z = 30;
Constants
A variable whose value cannot be changed after initialization can be declared using the final keyword.
Example:
final double PI = 3.14159;
Trying to change it will cause an error:
PI = 3.14; // Error
By convention, constants are written in uppercase letters.
final int MAX_SIZE = 100;
Variable Naming Rules
Java has rules for naming variables.
Valid Names
age studentName student_name age2 $total
Invalid Names
2age // Cannot start with a number student-name // Hyphen is not allowed class // Reserved keyword
Naming Conventions
Java commonly uses camelCase for variable names.
Good examples:
studentName totalMarks phoneNumber dateOfBirth
Avoid:
studentname StudentName student_name
The last two may be valid Java identifiers, but they do not follow the usual Java camelCase convention for variables.
Variable Scope
Scope means the part of the program where a variable can be accessed.
Example:
public class Main { public static void main(String[] args) { int age = 20; if (age >= 18) { System.out.println(age); } } }
The variable age can be accessed inside its surrounding method.
A variable declared inside a smaller block may only be available inside that block.
if (true) { int number = 10; System.out.println(number); } // System.out.println(number); // Error
Complete Example
public class Main { static String college = "Rajarshi Janak University"; String name; int age; public static void main(String[] args) { int year = 2026; Main student = new Main(); student.name = "Anuradha"; student.age = 20; System.out.println("Name: " + student.name); System.out.println("Age: " + student.age); System.out.println("College: " + college); System.out.println("Year: " + year); } }
Output:
Name: Anuradha Age: 20 College: Rajarshi Janak University Year: 2026
Summary
| Type | Declared Where | Belongs To |
|---|---|---|
| Local Variable | Inside method/block | Method or block |
| Instance Variable | Inside class, outside methods | Object |
| Static Variable | Inside class with static | Class |
Conclusion
Variables are an essential part of Java programming. They allow programs to store, access, modify, and process data. The three main types of variables are local, instance, and static variables. Understanding their declaration, scope, data types, and usage is important for learning Java.