Java Variables

Published: (January 12, 2026 at 09:55 AM EST)
1 min read
Source: Dev.to

Source: Dev.to

What is a Variable in Java?

A variable is a container used to store data values.

Types of Variables in Java

Local Variables

Example:

class Test {
    void display() {
        int number = 10; // local variable
        System.out.println(number);
    }
}

Instance Variables

Example:

class Student {
    int id;      // instance variable
    String name; // instance variable
}

Static Variables

Example:

class College {
    static String collegeName = "ABC College";
}

Simple Program Showing All Types

class Example {
    int instanceVar = 20;
    static int staticVar = 30;

    void show() {
        int localVar = 10;

        System.out.println(localVar);
        System.out.println(instanceVar);
        System.out.println(staticVar);
    }
}
Back to Blog

Related posts

Read more »