Types of Variables in Java: The Building Blocks of Clean Code
Updated on Jul 10, 2025 | 16 min read | 17.57K+ views
Share:
For working professionals
For fresh graduates
More
Updated on Jul 10, 2025 | 16 min read | 17.57K+ views
Share:
Table of Contents
Did you know? Since Java 10 and improved in Java 11, the var keyword allows developers to declare local variables without explicitly specifying their type, as the compiler infers it. This reduces verbosity and improves readability while maintaining static type safety. |
Variables in Java are core building blocks used to store and manage data in a program. Understanding the types of variables in Java, such as local, instance, and static, is essential for writing clean, efficient, and organized code. Each type serves a distinct purpose in managing data scope and memory, enabling developers to build robust and scalable applications.
In this blog, we break down the key types of variables in Java, providing simple explanations, real-world examples, and use cases to enhance your coding confidence and clarity.
The types of variables in Java, such as local, instance, and static, determine where your data is stored, how long it persists, and who can access it. Understanding these types helps you write cleaner, more efficient Java code. In this section, you'll learn what each type does, when to use it, and how it affects your program’s behavior.
Want to strengthen your Java basics like types of variables in Java, data structures, and OOP? Here are some courses you can consider to support your development journey and deepen your understanding of Java concepts.
Here’s a clear overview of the three main types of variables in Java:
Features |
Local Variable |
Instance Variable |
Static Variable |
Belongs To | Belongs to the method/block where it is declared | Belongs to one specific object | Belongs to the class shared by all objects |
Keyword Used | No special keyword needed | No special keyword needed | Uses the static keyword |
Memory | Stored in stack memory | Each object gets its copy in heap memory | One copy in memory shared across all objects |
How to Use | Used directly inside the method/block | Accessed using the object reference | Accessed using the class name (or object, but not preferred) |
When Created | Created when the method/block is called | Created when a new object is instantiated | Created when the class is loaded into memory |
Lifetime | Lives until the method/block execution ends | Lives as long as the object exists | Lives as long as the program or class is loaded |
Default Value | No default value; must be initialized before use | Gets default value (0, false, null, depending on type) | Also gets default value (0, false, null, depending on type) |
Used For | Temporary calculations or short-lived data | When you need unique values for each object | When a common/shared value is required for all objects |
This table gives a quick snapshot of the main types of variables in Java, helping you choose the right one based on scope, memory use, and purpose.
Also Read: Scope of a Variable In Java [With Coding Example]
Let’s explore each of the types of variables in Java to understand how they work, when to use them, and how they impact your code structure and behavior.
Local variables are declared inside methods, constructors, or blocks. You can only use them within that block. They're created when the block is entered and destroyed when it's exited.
Sample Code:
public class Example {
public void myMethod() {
int a = 10; // Local variable inside method
if (a == 10) {
int b = 20; // Local variable inside if block
System.out.println("Inside if block, b = " + b);
}
// System.out.println("Outside if block, b = " + b); // Error
System.out.println("Value of a = " + a);
}
public static void main(String[] args) {
Example example = new Example();
example.myMethod();
}
}
Code Explanation:
Output:
Inside if block, b = 20
Value of a = 10
Struggling to grasp Java fundamentals? upGrad’s Core Java Basics course breaks down key concepts like variables, loops, and OOP into easy-to-learn modules. Build real coding confidence, practice hands-on, and earn a certificate to boost your tech career.
Also Read: Difference Between Variable and Constant
Instance variables are declared inside a class but outside any method. They belong to each object, so every object gets its copy.
Sample Code:
public class Car {
String brand;
int year;
boolean isElectric;
public Car(String brand, int year, boolean isElectric) {
this.brand = brand;
this.year = year;
this.isElectric = isElectric;
}
public void displayInfo() {
System.out.println("Car Brand: " + brand);
System.out.println("Manufacture Year: " + year);
System.out.println("Is Electric: " + isElectric);
}
public static void main(String[] args) {
Car car1 = new Car("Tesla", 2020, true);
Car car2 = new Car("Toyota", 2015, false);
car1.displayInfo();
car2.displayInfo();
}
}
Code Explanation:
Output:
Car Brand: Tesla
Manufacture Year: 2020
Is Electric: true
Car Brand: Toyota
Manufacture Year: 2015
Is Electric: false
Also Read: Java Language History: Why Java Is So Popular and Widely Used Today
Static variables belong to the class, not instances. They're shared among all objects and exist as long as the program runs.
Sample Code:
public class Counter {
static int count = 0; // Static variable
public Counter() {
count++;
}
public static void displayCount() {
System.out.println("Number of instances created: " + count);
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
Counter c3 = new Counter();
Counter.displayCount();
}
}
Code Explanation:
Output:
Number of instances created: 3
Also Read: Careers in Java: How to Make a Successful Career in Java in 2025
Understanding the different types of variables in Java is just the first step. Now let’s explore how their scope and lifetime impact the behavior and structure of your code.
upGrad’s Exclusive Software Development Webinar for you –
SAAS Business – What is So Different?
In Java, scope tells you where a variable can be used, and lifetime defines how long it exists in memory. If you don’t manage them properly, you may run into bugs, memory leaks, or unexpected behavior.
By understanding how scope and lifetime work for local, instance, and static variables, you can write cleaner code. You can avoid conflicts and ensure each variable lives exactly as long as it needs to.
Here’s a quick comparison of the scope and lifetime of each type of variable in Java:
Variable Type |
Scope |
Lifetime |
Local | Within the block/method where it's defined | Created when block starts, ends when it finishes |
Instance | Throughout the class, using object reference | As long as the object exists |
Static | Across all objects of the class | From class loading to program termination |
Also Read: Exploring the 14 Key Advantages of Java: Why It Remains a Developer's Top Choice in 2025
Also Read: Abstract Class in Java – With Examples
Now that you understand the scope and lifetime of each variable type, let’s look at how local, instance, and static variables are used in real-world Java applications.
Understanding how local, instance, and static variables interact in real-world applications helps you build smarter, more scalable, and bug-free Java programs. Whether you’re tracking users, managing data, or performing operations, using the right variable type ensures memory efficiency, proper data encapsulation, and shared logic across objects.
Below are two practical examples that clearly and effectively integrate all three types of variables.
Example 1: Student Enrollment System with Batch Info
public class Student {
// Static variable shared across all students
static int totalStudents = 0;
// Instance variables specific to each student
String name;
String batch;
// Constructor to initialize student data
public Student(String name, String batch) {
this.name = name;
this.batch = batch;
totalStudents++; // Static variable updated
}
// Method to display student information
public void displayStudentInfo() {
// Local variable used only inside this method
String info = "Student Name: " + name + ", Batch: " + batch;
System.out.println(info);
}
// Static method to display total student count
public static void displayTotalStudents() {
System.out.println("Total Enrolled Students: " + totalStudents);
}
public static void main(String[] args) {
Student s1 = new Student("Aarav", "Batch A");
Student s2 = new Student("Mira", "Batch B");
s1.displayStudentInfo();
s2.displayStudentInfo();
Student.displayTotalStudents();
}
}
Code Explanation:
Output:
Student Name: Aarav, Batch: Batch A
Student Name: Mira, Batch: Batch B
Total Enrolled Students: 2
Example 2: Bank Account System with Transaction ID Generator
public class BankAccount {
// Static variable shared across all accounts to generate transaction IDs
static int nextTransactionId = 1000;
// Instance variable holds the current account balance
private double balance;
private String accountHolder;
// Constructor
public BankAccount(String accountHolder, double initialBalance) {
this.accountHolder = accountHolder;
this.balance = initialBalance;
}
// Deposit method
public void deposit(double amount) {
// Local variable used only for this transaction
int transactionId = nextTransactionId++;
balance += amount;
System.out.println("Deposited ₹" + amount + " | Transaction ID: " + transactionId);
}
// Withdraw method
public void withdraw(double amount) {
int transactionId = nextTransactionId++;
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrew ₹" + amount + " | Transaction ID: " + transactionId);
} else {
System.out.println("Insufficient balance | Transaction ID: " + transactionId);
}
}
public void displayBalance() {
System.out.println(accountHolder + "'s Account Balance: ₹" + balance);
}
public static void main(String[] args) {
BankAccount account1 = new BankAccount("Riya", 2000);
account1.deposit(500);
account1.withdraw(300);
account1.displayBalance();
BankAccount account2 = new BankAccount("Arjun", 1000);
account2.withdraw(1200);
account2.displayBalance();
}
}
Code Explanation:
Output:
Deposited ₹500.0 | Transaction ID: 1000
Withdrew ₹300.0 | Transaction ID: 1001
Riya's Account Balance: ₹2200.0
Insufficient balance | Transaction ID: 1002
Arjun's Account Balance: ₹1000.0
Now that you know how Java variables work, let’s look at best practices for initializing and managing them effectively.
Clean, maintainable Java code begins with how you define and use variables. Poor variable management leads to bugs, wasted memory, and confusing logic. Following modern best practices ensures better readability, performance, and debugging. Here's how to use the types of variables in Java effectively.
Variables are the backbone of any Java program. Knowing the differences between local, instance, and static variables gives you better control over your code’s behavior, memory usage, and data flow. Each type serves a specific purpose, whether it's limiting a variable's access to a block, storing object-specific data, or maintaining shared values across instances.
A clear understanding of these types of variables in Java helps you write efficient, bug-free code and makes your transition into object-oriented development smoother. If you're finding it tough to apply Java concepts in real projects, upGrad’s practical courses can help.
Here are some additional courses to support your learning:
Not sure how types of variables in Java apply to real-world coding? Connect with upGrad’s expert counselors or drop by your nearest upGrad offline center to discover a personalized learning path aligned with your goals.
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
References:
https://advancedweb.hu/new-language-features-since-java-8-to-21/
https://arxiv.org/pdf/2309.02594
900 articles published
Director of Engineering @ upGrad. Motivated to leverage technology to solve problems. Seasoned leader for startups and fast moving orgs. Working on solving problems of scale and long term technology s...
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources