Variables in Java: Declaration, Types, and Usage

In Java, variables are used to store data that can be referenced and manipulated during the execution of a program. The declaration and use of variables are fundamental aspects of Java programming.

1. Variable Declaration

In Java, a variable declaration consists of the type followed by the variable name. Optionally, you can also initialize the variable. Here’s the syntax:

type variableName;

Example of declaring variables:


// Variable Declaration Examples
int number; // integer type variable
String name; // string type variable
boolean isActive; // boolean type variable

// Variable Initialization
number = 5;
name = "Java";
isActive = true;
      

In the example above:

2. Types of Variables in Java

Java supports three types of variables:

Local Variables

Local variables are declared inside a method or block and can only be used within that method or block. They do not have a default value and must be initialized before they are used.


public class LocalVariableExample {
    public static void main(String[] args) {
        int number = 10; // Local variable, accessible only within this method
        System.out.println("Number: " + number);
    }
}
      

Static Variables

Static variables are declared with the static keyword and are shared among all instances of a class. They are initialized only once and retain their value throughout the program.


class StaticVariableExample {
    static int counter = 0; // Static variable, shared by all instances

    public StaticVariableExample() {
        counter++; // Each instance increments the static variable
    }

    public static void main(String[] args) {
        new StaticVariableExample();
        new StaticVariableExample();
        System.out.println("Counter: " + counter); // Output will be 2
    }
}
      

Global Variables (Instance Variables)

Global variables (often called instance variables) are declared within a class but outside any method or constructor. They are accessible by all methods of the class and are specific to each instance of the class. They are initialized to default values (e.g., 0 for integers, null for objects).


class GlobalVariableExample {
    int value = 5; // Global variable, accessible by all methods in the class

    public void displayValue() {
        System.out.println("Value: " + value);
    }

    public static void main(String[] args) {
        GlobalVariableExample obj = new GlobalVariableExample();
        obj.displayValue(); // Output will be 5
    }
}
      

3. Variable Initialization and Usage

After declaring a variable, you can initialize it (assign a value) and use it in your program. Here’s an example of how variables are used:


public class VariableExample {
    public static void main(String[] args) {
        int age = 25;
        String name = "John";
        boolean isStudent = true;
        
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Is Student: " + isStudent);
    }
}
      

In the above example:

The program will output:

Name: John
Age: 25
Is Student: true

4. Example of Variable Usage with Arithmetic Operations

Variables can be used in arithmetic operations as well. Here's an example:


public class ArithmeticExample {
    public static void main(String[] args) {
        int num1 = 10;
        int num2 = 5;
        int sum = num1 + num2;
        int product = num1 * num2;
        
        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
    }
}
      

The program will output:

Sum: 15
Product: 50