🔥 What is Variable in Java? Static Variable & Types of Variables Explained

🔥 What is Variable in Java? Static Variable & Types of Variables Explained

Introduction

If you are starting your journey with Java programming, one of the first concepts you’ll come across is variables. Think of a variable like a small container inside your computer’s memory that stores data. Without variables, writing any meaningful Java program would be impossible.

In this blog, we’ll break down:

  • What is a variable in Java?

  • What is a static variable?

  • The different types of variables in Java with examples.

Let’s dive in! 🚀


💡 What is a Variable in Java?

A variable in Java is simply a name that points to a memory location where data is stored.

👉 In simple words:

  • Variable = Name of a container.

  • Value = Content stored inside the container.

Example:

int age = 25;

String name = "John";


Here:

  • age is a variable that stores an integer value (25).

  • name is a variable that stores a string value (“John”).


⚡ Types of Variables in Java

In Java, variables are mainly classified into three categories:

1. Local Variables

  • Declared inside a method, constructor, or block.

  • Accessible only within that block.

  • Automatically destroyed once the method is finished.

✅ Example:

public class Demo {

    public void showMessage() {

        int number = 10;  // local variable

        System.out.println("Number: " + number);

    }

}


2. Instance Variables

  • Declared inside the class but outside any method.

  • Each object gets its own copy of the instance variable.

  • Often used to store object-specific properties.

✅ Example:

public class Student {

    String name;   // instance variable

    int age;       // instance variable

}


3. Static Variables (Class Variables)

  • Declared using the keyword static.

  • Shared among all objects of the class (only one copy exists).

  • Useful for values that should be common for all objects.

✅ Example:

public class Student {

    String name;

    int age;

    static String schoolName = "ABC School";  // static variable

}


👉 Here, schoolName will be the same for all students.


🔑 Why Use Static Variables?

Static variables are powerful when you want to save memory and keep values consistent across all objects.

✔️ Advantages:

  • Saves memory (one copy for all objects).

  • Easy to access without creating an object.

👉 Example:

public class Counter {

    static int count = 0;


    Counter() {

        count++;

        System.out.println("Object number: " + count);

    }


    public static void main(String[] args) {

        new Counter();

        new Counter();

        new Counter();

    }

}


Output:

Object number: 1  

Object number: 2  

Object number: 3  



📊 Quick Recap of Variable Types

Variable Type

Scope

Stored Where?

Life Cycle

Local

Inside methods/blocks

Stack Memory

Until method ends

Instance

Per object

Heap Memory

Until object destroyed

Static

Shared across all objects

Method Area

Until program ends

Variables are the building blocks of Java programs. Whether it’s a local variable, an instance variable, or a static variable, each plays a unique role in handling data efficiently.

👉 If you want to become strong in Java, practice writing small programs using all three types of variables.

💬 Did this article help you understand Java variables better? Drop your thoughts in the comments, share it with your friends, and don’t forget to follow this blog for more programming tutorials! 🚀



Previous Post Next Post