Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Are Static Local Variables Allowed in Java?
Next article icon

Are Static Local Variables Allowed in Java?

Last Updated : 16 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Java, there are different types of variables, each with its own behavior and scope. Understanding these variables plays a very important role. In this article, we will discuss the concept of static with local variables, and we will also discuss in Java static local variables are allowed or not.

What are Local Variables?

Local Variables are those variables that are declared inside the body of a method, a constructor, or a block in Java. These variables can only be accessed within the block where they are declared. Once the block ends, the local variables declared inside that block are destroyed, and they free up the memory.

Example: This example demonstrates where we can access the local variables.

Java
// Demonstrating the working of local variable public class Geeks {     public void Method() {                   // a is a local variable         int a = 10;                   // a can be accessed here         System.out.println(a);       } } 

Explanation: In the above example, the variable a is a local variable, and we can use this method only inside the Method(), we cannot access this local variable a outside this method.

Now, we already understand the working of a Local variable, now let's discuss about static keyword in Java.

Static Keyword

In Java, the static keyword is used to define class-level variables and methods. Static variables and methods belong to the class, it means we can access them without creating an object of the class.

Example: In this example, we have declared a variable x as static, it means we can directly access this method without creating an object of a class.

Java
// Demonstrating how static variables works in Java public class Geeks {          // Static variable     static int x = 0;        public static void main(String[] args) {                  // No object needed, we can access a directly         System.out.println(x);       } } 

Output
0 


Static Local Variables In Java

Static Local variables are not allowed in Java because we know that static variables belong to a class and are shared by all the objects of the class, on the other hand, local variables only exist while the method is running, local variables are destroyed onces the method is completed. Local variables are temporary but static variables are permanent for the class that's why static local variables does not fit in Java.

Let's go through a real-world example:

Real-World Example: Let's take an example of a office Printer and a Notepad.

For Static Variable,

  • The printer (Static Variable) which is shared by all the office employees.
  • Only one printer exists for the entire office (Class).
  • Every employee can use it.
  • It stays available even when employees come and go.

For Local Variable,

  • Suppose there is a personal notepad on your desk.
  • So, only you can use it.
  • It will get thrown away when you leave for the day (here, method ends)
  • It cannot be turned into a printer (cannot be static).

That is why local variables cannot be static in Java.

Example: This example demonstrates that static local variables are not allowed.

Java
// Demonstrating how defining static local // variables cause error public class Geeks {          public void Method() {                  // This will cause a compile-time error         static int a = 0;           a++;         System.out.println(a);     } } 

Explanation: In the above example, we are trying to use static with a local variable, Java will give a compilation error because Java does not allow local variables to be static, the static keywords should only be used for class level variables or methods.


Next Article
Are Static Local Variables Allowed in Java?

K

kartik
Improve
Article Tags :
  • Java
  • Java-Output
  • java-basics
Practice Tags :
  • Java

Similar Reads

    Final Local Variables in Java
    In Java, a local variable is a variable, which is declared inside a method. Local variables are only accessible within the method in which they are declared, other methods in the class do not know anything about that variable. When we declare a local variable, we need to initialize it first before u
    3 min read
    Final Static Variable in Java
    When the value of a variable is not varied, then it is not a good choice to go for an instance variable. At that time, we can add a static modifier to that variable. Whenever we declare a variable as static, then at the class level, a single variable is created which is shared with the objects. Any
    3 min read
    Static Variables in Java
    In Java, when a variable is declared with the static keyword. Then, a single variable is created and shared among all the objects at the class level. Static variables are, essentially, global variables. All instances of the class share the same static variable. These are the main scenarios when we u
    3 min read
    Static and Non Static Blank Final Variables in Java
    In Java, a variable provides us with named storage that our programs can manipulate. In Java, a blank final variable is a final variable that is declared but not immediately initialized. It must be assigned exactly once before use. When a variable is declared with static, it becomes a static blank f
    6 min read
    Difference between static and non-static variables in Java
    There are three types of variables in Java: Local VariablesInstance VariablesStatic Variables The Local variables and Instance variables are together called Non-Static variables. Hence it can also be said that the Java variables can be divided into 2 categories: Static Variables: When a variable is
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences