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 »

Static and Non-Static Variables in Java

Non-Static Instance Variables In Java, a non‑static variable is declared inside a class but outside any method, without using the static keyword. Each object o...

Interface vs Abstract class

Interface or Abstract Class? They are very similar, and therefore it can be confusing to know when to use each one. I’m going to answer this question in this a...