Variables

Declaration, initialization, and scope

Interview Relevant: Variable scope and lifecycle in interviews
7 min read

What are Variables?

A variable is a named container that holds a value. It reserves a space in memory to store data. Every variable has three components: name, type, and value.

Variable Declaration and Initialization

Declaration: Tells Java to reserve memory and create the variable.

Initialization: Assigns an initial value to the variable.

Syntax

dataType variableName = value;

Variable Scope

Scope determines where a variable can be accessed in your code. Java has four types of scopes:

  • Local Scope: Variables declared inside methods. Accessible only within that method.
  • Method Parameter Scope: Parameters are local to the method.
  • Instance Variable Scope: Variables declared in a class but outside methods. Accessible to all methods in the class.
  • Static/Class Variable Scope: Shared by all instances of the class.

āš ļø Important: Local variables are NOT automatically initialized. You must initialize them before use!

Variable Types in a Class

  • Instance Variables: Each object has its own copy (default initialized)
  • Static Variables: Shared across all objects (default initialized)
  • Local Variables: Declared in methods (NOT initialized by default)
  • Parameters: Arguments passed to methods

Code Examples

Different ways to declare and initialize variables

java
1// Variable declaration and initialization
2int age = 25;              // declare and initialize
3String name = "John";
4double salary = 50000.50;
5boolean isActive = true;
6
7// Multiple declarations
8int x = 10, y = 20, z = 30;
9
10// Declaration without initialization
11int count;  // declared but not initialized
12count = 100; // later initialization

Different variable scopes in a Java class

java
1public class VariableScope {
2    // Instance variable - belongs to each object
3    private String name;
4    
5    // Static variable - shared by all objects
6    public static int totalStudents = 0;
7    
8    public void displayInfo() {
9        // Local variable - only accessible in this method
10        String message = "Hello";
11        System.out.println(message);
12    }
13    
14    public void greet(String greeting) {
15        // greeting is a parameter (local to this method)
16        System.out.println(greeting);
17    }
18}

Demonstrating block scope - variables are only accessible within their block

java
1public class ScopeExample {
2    public static void main(String[] args) {
3        int x = 10;  // local to main method
4        
5        {
6            int y = 20;  // local to this block
7            System.out.println(x); // OK - x is visible here
8            System.out.println(y); // OK - y is visible here
9        }
10        
11        System.out.println(x); // OK
12        // System.out.println(y); // ERROR - y is out of scope
13    }
14}

Use Cases

  • Creating variables to store program data and state
  • Understanding variable lifetime and memory management
  • Avoiding variable shadowing and scope conflicts
  • Managing class-level vs method-level data
  • Organizing variable access with proper scope

Common Mistakes to Avoid

  • Using variables before initialization (causes compiler error for local variables)
  • Declaring same variable twice in same scope (shadowing)
  • Assuming local variables are initialized (they're not)
  • Trying to access local variables outside their scope
  • Modifying static variables thinking they're instance-specific
  • Not understanding the difference between parameter and local variable