1. Introduction
A variable is a named memory location used to store data in a Java program.
For example:
int age = 21;
String name = "Abishek";
double salary = 25000.50;
In this example:
-
agestores an integer value. -
namestores a String value. -
salarystores a decimal value.
Variables are important because they allow us to store, access, and modify data while a program is running.
2. Types of Variables in Java
Java has three main types of variables:
- Local Variable
- Instance Variable
- Static Variable
A. Local Variable
A variable declared inside a method, constructor, or block is called a local variable.
Example:
class Student {
void display() {
int age = 21;
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Output:
21
Explanation:
-
ageis declared inside thedisplay()method. - Therefore, it is a local variable.
- It can only be accessed within its applicable scope.
- A local variable does not receive a default value. We must initialize it before using it.
B. Instance Variable
A variable declared inside a class but outside methods, constructors, and blocks, without the static keyword, is called an instance variable.
Example:
class Student {
String name;
int age;
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Abishek";
s1.age = 21;
System.out.println(s1.name);
System.out.println(s1.age);
}
}
Output:
Abishek
21
Explanation:
-
nameandageare instance variables. - They belong to the object.
- Each object gets its own copy of instance variables.
- Instance variables receive default values if they are not explicitly initialized.
C. Static Variable
A variable declared inside a class using the static keyword is called a static variable.
Example:
class Student {
static String college = "ABC College";
public static void main(String[] args) {
System.out.println(Student.college);
}
}
Output:
ABC College
Explanation:
-
collegeis a static variable. - It belongs to the class rather than individual objects.
- All objects of the class share the same static variable.
- We can access it using the class name.
3. Difference Between Local, Instance, and Static Variables
| Feature | Local Variable | Instance Variable | Static Variable |
|---|---|---|---|
| Declaration | Inside method, constructor, or block | Inside class, outside methods | Inside class using static
|
| Belongs to | Its scope | Object | Class |
| Default value | No default value | Yes | Yes |
| Access | Within its scope | Through an object | Through class name |
| Shared | No | No | Yes |
4. Real-World Example
Consider a Student Management System.
Imagine a college has 100 students.
Each student has:
- A different name.
- A different age.
- The same college name.
We can represent this using Java variables.
class Student {
String name; // Instance variable
int age; // Instance variable
static String college = "ABC"; // Static variable
void display() {
int mark = 90; // Local variable
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("College: " + college);
System.out.println("Mark: " + mark);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Abishek";
s1.age = 21;
s1.display();
}
}
Output:
Name: Abishek
Age: 21
College: ABC
Mark: 90
In this example:
-
nameandageare instance variables because each student can have different values. -
collegeis a static variable because all students share the same college. -
markis a local variable because it is declared inside thedisplay()method.
5. Important Points to Remember
- Java has three main types of variables: Local, Instance, and Static.
- Local variables must be initialized before use.
- Instance variables belong to objects.
- Static variables belong to classes.
- Each object has its own instance variables.
- Static variables are shared among objects of the same class.
- Local variables are accessible only within their scope.
6. Conclusion
Variables are one of the fundamental concepts in Java programming. They help us store and manage data in a program.
Understanding the differences between local, instance, and static variables is important for learning Object-Oriented Programming (OOP) in Java.
By understanding these three types, we can write better-organized and reusable Java programs.
Top comments (0)