Java – Types of Variables
Java – Types of Variables – In Java, variables are used to store data values. Depending on where and how they are declared, variables in Java can be classified into several types. This tutorial covers the following types of variables:
- Instance Variables
- Static Variables
- Local Variables
- Method Parameters
Each section below provides detailed descriptions and example programs to help you understand and effectively use each type of variable in Java.
1 Instance Variables
Instance variables are declared inside a class but outside any method, constructor, or block. Each object of the class has its own copy of the instance variable.
Example Program:
public class Car {
// Instance variable
int speed = 60;
public void displaySpeed() {
System.out.println("Car Speed: " + speed + " km/h");
}
public static void main(String[] args) {
Car car1 = new Car();
Car car2 = new Car();
// Changing the instance variable for car2
car2.speed = 80;
car1.displaySpeed(); // Expected output: Car Speed: 60 km/h
car2.displaySpeed(); // Expected output: Car Speed: 80 km/h
}
}
Output:
Car Speed: 60 km/h
Car Speed: 80 km/h
This program uses the Car
class to demonstrate instance variables. The speed
variable is an instance variable, meaning that each Car
object (here, car1
and car2
) has its own separate copy. Changing the speed of car2
does not affect the speed of car1
, as seen in the output.
2 Static Variables
Static variables, also known as class variables, are declared with the static
keyword inside a class but outside any method, constructor, or block. They are shared among all instances of the class.
Example Program:
public class Bicycle {
// Static variable
static int gear = 3;
public static void main(String[] args) {
// Accessing the static variable without creating an object
System.out.println("Default Gear: " + gear);
// Creating objects of Bicycle
Bicycle bike1 = new Bicycle();
Bicycle bike2 = new Bicycle();
// Changing the static variable using one object
bike1.gear = 5;
System.out.println("Gear from bike2: " + bike2.gear);
}
}
Output:
Default Gear: 3
Gear from bike2: 5
In this program, the Bicycle
class demonstrates the use of a static variable, gear
. Since gear
is static, it is shared across all instances of Bicycle
. When the value of gear
is changed through bike1
, the change is reflected when accessing it from bike2
, as shown in the output.
3 Local Variables
Local variables are declared within a method, constructor, or block. They are created when the method is invoked and destroyed once the method finishes execution. Local variables must be explicitly initialized before use.
Example Program:
public class Truck {
public static void main(String[] args) {
// Local variable declaration and initialization
int loadWeight = 1000;
System.out.println("Truck Load Weight: " + loadWeight + " kg");
}
}
Output:
Truck Load Weight: 1000 kg
This Truck
class example shows a local variable loadWeight
defined within the main
method. Since it is local, it is only accessible within the method. The program prints the truck’s load weight, illustrating how local variables function in Java.
4 Method Parameters
Method parameters are variables passed to methods. They are used as local variables within the method and hold the values provided during the method call.
Example Program:
public class Calculator {
// Method with parameters
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(15, 25);
System.out.println("Sum: " + result);
}
}
Output:
Sum: 40
The Calculator
class demonstrates the use of method parameters. The add
method takes two parameters (a
and b
), adds them, and returns the result. When called from the main
method with the values 15 and 25, the method returns their sum, which is printed to the console.
Conclusion
In this Java Tutorial, we explored the different types of variables in Java. We covered instance variables, static variables, local variables, and method parameters, providing detailed explanations, example programs, and output for each. Understanding these variable types is fundamental for writing efficient and well-structured Java programs.