Java variables are of three types:
- Local variables
- Instance variables
- Class/Static variables
2. Java Local Variables
- Local variables are declared within methods, constructors, or blocks.
- Local variables are created when the method, constructor, or block is entered, and the variables are destroyed when it exits.
- Access modifiers cannot be used for local variables.
- Local variables can be used only within the method, constructor, or block in which they are declared.
- Local variables are implemented at the inner level.
- There is no default value for local variables, so they must be declared and initialized before being used for the first time.
Source Code:
public class LocalVariable {
public static void main(String[] args) {
int age = 25; // Local variable declared and initialized in main method
System.out.println("Age: " + age);
// Calling a method that uses its own local variables
calculateSquare(5);
// Using local variables in a loop
for (int i = 1; i <= 3; i++) { // 'i' is a local variable for this loop
System.out.println("Loop iteration: " + i);
}
}
public static void calculateSquare(int number) {
int square = number * number; // Local variable to this method
System.out.println("Square of " + number + " is: " + square);
}
}Result:
Watch the video:
Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

