Java

Difference Between Local, Global and Static Variables in Java – Complete Guide for Beginners

Local, Global & Static Variables in Java 🚀

By Bhau Automation • Java Variables Tutorial for Beginners

🎯 What You Will Learn

  • What are variables in Java?
  • Difference between local, global and static variables
  • Variable declaration and initialization
  • Memory allocation of variables
  • Java variable examples
  • Java interview questions on variables
💡 Variables are one of the most important concepts in Java programming and are used in every Java application.

📌 What is a Variable in Java?

A variable in Java is a container used to store data values during program execution.

Variables help programs:

  • Store information temporarily
  • Perform calculations
  • Manage application data
  • Control program flow

🛠️ Variable Declaration in Java

Syntax of variable declaration:

dataType variableName = value;

Example:

int age = 25;

📍 What is Local Variable?

A local variable is declared inside a method, constructor, or block and can only be accessed within that scope.

💻 Local Variable Example

public class LocalVariableDemo {

    public void display() {

        int number = 10;

        System.out.println(number);
    }
}

🌍 What is Global Variable?

A global variable (instance variable) is declared inside class but outside methods. It is accessible throughout the class using objects.

💻 Global Variable Example

public class GlobalVariableDemo {

    int salary = 50000;

    public void show() {

        System.out.println(salary);
    }
}

⚡ What is Static Variable?

A static variable belongs to the class instead of object. Only one copy of static variable is shared among all objects.

💻 Static Variable Example

public class StaticVariableDemo {

    static String company = "Bhau Automation";

    public static void main(String[] args) {

        System.out.println(company);
    }
}

📊 Difference Between Local, Global & Static Variables

Feature Local Variable Global Variable Static Variable
Declaration Inside method Inside class Using static keyword
Scope Within method only Entire class Shared across objects
Memory Allocation Stack Memory Heap Memory Method Area

🧠 Variable Initialization

Variable initialization means assigning value to variable before using it.

int marks = 90;

💾 Memory Allocation of Variables

  • Local variables → Stack memory
  • Instance variables → Heap memory
  • Static variables → Method area memory

🌍 Real-World Use Cases

  • Student management systems
  • Banking applications
  • Selenium automation frameworks
  • Enterprise Java applications
  • Cyber security tools

❓ Common Interview Questions

Q: What is the difference between local and global variable?

A: Local variables are declared inside methods, while global variables are declared inside class outside methods.

Q: Why do we use static variables?

A: Static variables are shared across all objects and save memory.

Q: Where are local variables stored?

A: Local variables are stored in stack memory.

🎥 Watch Complete Video Tutorial

👉 Watch Java Variables Tutorial

🎓 Key Takeaways

  • Variables store data values in Java
  • Local variables work inside methods only
  • Global variables belong to objects
  • Static variables are shared across objects
  • Important Java interview topic for beginners
Pro Tip: Practice variable declaration and initialization programs regularly to strengthen Java programming fundamentals.

🚀 Created with ❤️ by Bhau Automation