Last Updated : 23 Jul, 2025
In Java, variables are containers that store data in memory. Understanding variables plays a very important role as it defines how data is stored, accessed, and manipulated.
Key Components of Variables in Java:
A variable in Java has three components, which are listed below:
Note: There are three types of variables in Java: Local, Instance and Static.
Example: The below example demonstrates the variable declaration in Java
Java
// Demonstarting how to declare and use a variable in Java
class Geeks {
public static void main(String[] args) {
// Declaring and initializing variables
// Integer variable
int age = 25;
// String variable
String name = "GeeksforGeeks";
// Double variable
double salary = 50000.50;
// Displaying the values of variables
System.out.println("Age: " + age);
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
Age: 25 Name: GeeksforGeeks Salary: 50000.5How to Declare Java Variables?
The image below demonstrates how we can declare a variable in Java:
Variable DeclarationFrom the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are:
In this way, a name can only be given to a memory location. It can be assigned values in two ways:
It can be perceived with the help of 3 components explained above:
Variable InitializationExample: Here, we are initalizing variables of different types like float, int and char.
Java
// Demonstrating how to intialize variables
// of different types in Java
class Geeks{
public static void main(String[] args) {
// Declaring and initializing variables
// Initializing float variable
float si = 5.5f;
// Initializing integer variables
int t = 10;
int s = 20;
// Initializing character variable
char var = 'h';
// Displaying the values of the variables
System.out.println("Simple Interest: " + si);
System.out.println("Speed: " + s);
System.out.println("Time: " + t);
System.out.println("Character: " + var);
}
}
Simple Interest: 5.5 Speed: 20 Time: 10 Character: hTypes of Java Variables
Now let us discuss different types of variables which are listed as follows:
Let us discuss the traits of every type of variable listed here in detail.
1. Local VariablesA variable defined within a block or method or constructor is called a local variable.
Example: This example show how a local variable is declared and used inside the main method and it can not be used outside of it.
Java
// 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;
// This variable is local to this main method only
System.out.println("Local Variable: " + var);
}
}
Local Variable: 10
Example: This example demonstrates that local variables are only accessible within the block in which they are declared
Java
// Java Program to show the use of
// Local Variables
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);
}
}
x = 10 message = Hello, world! x is greater than 5 Iteration 0 Iteration 1 Iteration 22. Instance Variables
Instance variables are known as non-static variables and are declared in a class outside of any method, constructor, or block.
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.
Java
// Java Program to show the use of
// Instance Variables
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);
}
}
Geek name is: Sweta Dash Default value for int is 0 Default value for Integer is: null3. Static Variables
Static variables are also known as class variables.
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.
Java
// Java Program to show the use of
// Static variables
import java.io.*;
class Geeks {
// Declared static variable
public static String geek = "Sweta Dash";
public static void main(String[] args)
{
// geek variable can be accessed without object
// creation Displaying O/P Geeks.geek --> using the
// static variable
System.out.println("Geek Name is: " + Geeks.geek);
// static int c = 0;
// above line, when uncommented,
// will throw an error as static variables cannot be
// declared locally.
}
}
Geek Name is: Sweta DashInstance Variables vs Static Variables
Now let us discuss the differences between the Instance variables and the Static variables:
Syntax: Static and instance variables
Common Mistakes to Avoidclass Geeks
{
// Static variable
static int a;
// Instance variable
int b;
}
The common mistakes that can occur when working with variables in Java are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4