Are static local variables allowed in Java?


Unlike C, C++, Java does not allow static local variables. The compiler will throw the compilation error.

Example

Create a java class named Tester.

Tester.java

Live Demo

public class Tester {
   public static void main(String args[]) {
      static int a = 10;
   }
}

Output

Compile and Run the file to verify the result.

Tester.java:3: error: illegal start of expression                
   static int a = 10;

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 18-Jun-2020

800 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements