Variable and Types of Variables

Java 8 min min read Updated: Mar 31, 2026 Beginner
Variable and Types of Variables
Beginner Topic 11 of 25

Variables and Types of Variables

Variables are one of the most fundamental concepts in Java programming. They are used to store data in memory so that it can be used, modified, and processed during program execution.

Every Java program uses variables to hold values such as numbers, text, objects, and results of computations. Understanding variables and their types is essential for writing correct and efficient Java programs.

Key Concept: A variable is a named memory location used to store data. Different types of variables exist based on where they are declared and how they behave.

What is a Variable?

A variable is a container that holds a value. The value stored in a variable can change during program execution (unless it is declared as final).

Example:

java int age = 25; String name = "Amit";

In the above example:

  • age is a variable storing an integer value
  • name is a variable storing a string value

Syntax of Variable Declaration

java dataType variableName = value;

Example

java int marks = 90; double salary = 50000.50; char grade = 'A';

Steps Involved in Variables

1. Declaration

Declaring a variable means specifying its type and name.

java int age;

2. Initialization

Assigning an initial value to the variable.

java age = 25;

3. Declaration + Initialization

java int age = 25;

Rules for Naming Variables

  • Must start with a letter, underscore (_), or dollar sign ($)
  • Cannot start with a number
  • Cannot use Java keywords (e.g., int, class)
  • Should be meaningful and descriptive
  • Java is case-sensitive (age and Age are different)

Valid Examples

java int age; double totalSalary; String studentName;

Invalid Examples

java int 1age; // Invalid int class; // Invalid (keyword) int total-salary; // Invalid (hyphen not allowed)

Types of Variables in Java

Based on where variables are declared and how they behave, Java variables are classified into three types:

  • Local variables
  • Instance variables
  • Static variables

1. Local Variables

Local variables are declared inside a method, constructor, or block. They are created when the method is called and destroyed when the method finishes execution.

Example

java public class Demo { public static void main(String[] args) { int x = 10; // local variable System.out.println(x); } }

Characteristics of Local Variables

  • Declared inside methods or blocks
  • Accessible only within that method/block
  • No default value (must be initialized before use)
  • Stored in stack memory

Example of Error

java int x; System.out.println(x); // Error: not initialized

2. Instance Variables

Instance variables are declared inside a class but outside any method. They belong to an object (instance) of the class.

Example

java class Student { int age; // instance variable void display() { System.out.println(age); } }

Characteristics of Instance Variables

  • Declared inside class but outside methods
  • Each object gets its own copy
  • Have default values (0, null, false)
  • Stored in heap memory

Example with Object

java public class Main { public static void main(String[] args) { Student s = new Student(); System.out.println(s.age); // default value 0 } }

3. Static Variables

Static variables are declared using the static keyword. They belong to the class rather than any specific object.

Example

java class Student { static String college = "ABC College"; }

Characteristics of Static Variables

  • Declared using static
  • Shared among all objects
  • Only one copy exists
  • Stored in method area

Example

java public class Main { public static void main(String[] args) { System.out.println(Student.college); } }

Difference Between Local, Instance, and Static Variables

Feature Local Variable Instance Variable Static Variable
Declared in Method/block Class Class (with static)
Scope Within method Within object Entire class
Default value No Yes Yes
Memory Stack Heap Method area
Shared No No Yes

Example Combining All Variable Types

java class Demo { int instanceVar = 10; static int staticVar = 20; void show() { int localVar = 30; System.out.println("Instance: " + instanceVar); System.out.println("Static: " + staticVar); System.out.println("Local: " + localVar); } } public class Main { public static void main(String[] args) { Demo d = new Demo(); d.show(); } }

Default Values of Variables

Type Default Value
int 0
double 0.0
boolean false
char '\u0000'
Object null

Note: Default values apply only to instance and static variables, not local variables.

Final Variables

A variable declared with final cannot be reassigned once initialized.

java final int MAX = 100; // MAX = 200; // Error

Variable Scope

Scope defines where a variable can be accessed.

  • Local variable → only within method
  • Instance variable → throughout object
  • Static variable → throughout class

Common Mistakes

  • Using uninitialized local variables
  • Confusing static and instance variables
  • Using poor variable names
  • Declaring variables with wrong data type

Best Practices

  • Use meaningful variable names
  • Follow camelCase naming convention
  • Minimize variable scope
  • Use final for constants
  • Avoid unnecessary global/static variables

Interview-Oriented Points

  • Variables store data in memory
  • Three main types: local, instance, static
  • Local variables have no default value
  • Static variables are shared across objects
  • Instance variables belong to objects
  • Java is strongly typed

Conclusion

Variables are the backbone of any Java program. Understanding different types of variables and their behavior helps in writing efficient and structured code.

From simple local variables to shared static variables, each type plays a crucial role in Java programming and object-oriented design.

Quick Summary: Variables store data in Java, and they are categorized as local, instance, and static based on their scope and behavior.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators