Java – why do you accept static final variables in local classes?

I use Google to search widely but to no avail I can't seem to revolve around this concept Why do local classes accept static final fields? For example:

public void sayGoodbyeInEnglish() {

        class EnglishGoodbye {
            public static final String farewell = "Bye bye";
            public void sayGoodbye() {
                System.out.println(farewell);
            }
        }
        System.out.println(EnglishGoodbye.farewell);
        EnglishGoodbye myEnglishGoodbye = new EnglishGoodbye();
        myEnglishGoodbye.sayGoodbye();
    }

Does englishgoodbye allow variables to say goodbye in class? Why? I'm confused. Why are static variables allowed but not available? I understand why it cannot access closed range members unless they are compiler time constants, because these variables no longer exist when the function ends, but the class may not exist yes? I'm just confused about it

thank you!

Solution

Generally speaking, this is not

But farewell is a special static final: a value is a constant, defined by JLS 15.28 This means that it is not initialized at this location, which is actually not allowed for non static classes (including local classes), such as every JLS 8.1 three

JLS in 8.1 3 (and shown in BOLD) (note the "unless" section):

If you change the line to remove the final modifier or make the expression non constant (for example, new string ("Bye Bye"), you will get the expected compilation error:

Test.java:5: error: Illegal static declaration in inner class EnglishGoodbye
            public static final String farewell = new String("Bye bye");
                                       ^
  modifier 'static' is only allowed in constant variable declarations
1 error

A little more:

This is allowed because the compiler deals specifically with constant variables In particular, it allows them to be inlined – the resulting bytecode has no farewell fields at all! If you decompile the class (javap - C yourclassname), you will see the following:

public void sayGoodbyeInEnglish();
  Code:
     0: getstatic     #2                  // Field java/lang/System.out:Ljava/io/PrintStream;
     3: ldc           #3                  // String Bye bye
     5: invokevirtual #4                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
     ...

The above corresponds to this line:

System.out.println(EnglishGoodbye.farewell);

This is a bit daunting, but please note that "3:" OK The program does not load the value of the field, but its loading constant #3 (it annotates the string "Bye Bye" in the comment) (you can see the list of bytecode on Wikipedia)

Because a variable is a constant variable (not a "real" static member), it can be inlined in the code, and it doesn't matter where it is defined - the life cycle of a variable is basically the life cycle of the whole JVM, not any class or instance, so it can be declared anywhere

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>