0

I'm trying to declare and initialize local String variable as static , but there is a compilation error showing Illegal modifier static. Why is it so?

Here is my code:

public class StringInstance {
    public static void main(String[] args) {
    static String s = "a";

    if(s instanceof String){
        System.out.println("Yes it is");
     }
  }
}
Chris Martin
  • 30,334
  • 10
  • 78
  • 137
faizal vasaya
  • 517
  • 3
  • 12

1 Answers1

2

The String you are declaring will be static because it's scope is static, so you don't need the static modifier. But if you want to declare it static outside the scope of main() then do it like this:

public class StringInstance {
  static String s = "a";
    public static void main(String[] args) {

    if(s instanceof String){
        System.out.println("Yes it is");
    }
  }
}
Jonah Haney
  • 521
  • 3
  • 12