Variables in Java are used to store data values during program execution. Each variable is associated with a specific data type that defines the kind of value it can hold. Variables help in performing operations, storing user input, and managing data in a program.
- Java variables must be declared before they are used in a program.
- Variable names should follow Java naming conventions for better readability.
- Different types of variables in Java include local variables, instance variables, and static variables.
Types of Variables
Java variables are divided into different types based on where they are declared and how they are used in a program.

Local VariablesÂ
A variable defined within a block, method, or constructor is referred to as a local variable.Â
- The Local variable is created at the time of declaration and destroyed when the function completes its execution.
- The scope of local variables exists only within the block in which they are declared.
- We first need to initialize a local variable before using it within its scope.
Example: Java Program to show the use of local variables
import java.io.*;
class Geeks {
public static void main(String[] args)
{
// Declared a Local Variable
int var = 10;
System.out.println("Local Variable: " + var);
}
}
Output
Local Variable: 10
Example: This example demonstrates that local variables are only accessible within the block in which they are declared
import java.io.*;
public class Geeks {
public static void main(String[] args)
{
// x is a local variable
int x = 10;
// message is also a local
// variable
String message = "Hello, world!";
System.out.println("x = " + x);
System.out.println("message = " + message);
if (x > 5) {
// result is a
// local variable
String result = "x is greater than 5";
System.out.println(result);
}
// Uncommenting the line below will result in a
// compile-time error System.out.println(result);
for (int i = 0; i < 3; i++) {
String loopMessage
= "Iteration "
+ i; // loopMessage is a local variable
System.out.println(loopMessage);
}
// Uncommenting the line below will result in a
// compile-time error
// System.out.println(loopMessage);
}
}
Output
x = 10 message = Hello, world! x is greater than 5 Iteration 0 Iteration 1 Iteration 2
Instance Variables
Instance variables are known as non-static variables and are declared in a class outside of any method, constructor, or block. Instance variables are created when an object is instantiated and destroyed when the object is destroyed.
- Can have access specifiers; default access is used if none is specified.
- Accessed only through objects of the class.
- Instance Variables can be initialized using constructors or instance blocks.
Example: This example demonstrates the use of instance variables, which are declared within a class and initialized via a constructor, with default values for uninitialized primitive types.
import java.io.*;
class Geeks {
// Declared Instance Variable
public String geek;
public int i;
public Integer I;
public Geeks()
{
// Default Constructor
// initializing Instance Variable
this.geek = "Sweta Dash";
}
// Main Method
public static void main(String[] args)
{
// Object Creation
Geeks name = new Geeks();
// Displaying O/P
System.out.println("Geek name is: " + name.geek);
System.out.println("Default value for int is "+ name.i);
// toString() called internally
System.out.println("Default value for Integer is: "+ name.I);
}
}
Output
Geek name is: Sweta Dash Default value for int is 0 Default value for Integer is: null
Static Variables
Static variables in Java are variables declared with the static keyword inside a class but outside any method. They are shared among all objects of the class and exist for the entire lifetime of the program.Â
- There is only one copy of a static variable for the entire class, and all objects share it
- Static variable are created at program start and destroyed when the program ends.
- Can be initialized using static blocks.
Example: This example demonstrates the use of static variables, which belong to the class and can be accessed without creating an object of the class.
import java.io.*;
class Geeks {
// Static variable
static String geek = "Sweta Dash";
public static void main(String[] args)
{
// Access static variable without creating an object
System.out.println("Geek Name is: " + Geeks.geek);
// static int c = 0;
// Error: static variables cannot be declared inside a method
}
}
Output
Geek Name is: Sweta Dash
Local vs Instance vs Static Variables
| Feature | Local Variable | Instance Variable | Static Variable |
|---|---|---|---|
| Declared | Inside method/block | Inside class, outside methods | Inside class with static keyword |
| Scope | Only within the block/method | Across class methods (non-static) | Shared across all objects of class |
| Lifetime | Exists only during method/block | Exists as long as object exists | Exists throughout program execution |
| Number of Copies | Each method call creates new | Each object has its own copy | Only one copy for the class |
| Default Value | No default; must initialize | Default based on type (0, null) | Default based on type (0, null) |
| Access | Only within method/block | Through objects | Through class name (or object) |
| Initialization | Required before use | Optional; can use constructors | Optional; can use static blocks |