Home PHP C# C++ Android Java Javascript Python IOS SQL HTML Categories

Why static final variables are accepted in local classes?

It's not, in general.

But farewell is a special kind of static final: one whose value is a constant, as defined by JLS 15.28. That means that it's not being initialized at that location, which is what's actually disallowed in non-static classes (including local classes), as per JLS 8.1.3.

The JLS states this explicitly (and in bold font) in 8.1.3 (note the "unless" part):

It is a compile-time error if an inner class declares a member that is explicitly or implicitly static, unless the member is a constant variable (ยง4.12.4).

If you change that line to either remove the final modifier or make the expression non-constant (for example, new String("Bye bye")), then you'll get the compilation error you expect: 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 bit more:

The reason this is allowed is that constant variables are treated specially by the compiler. In particular, it's allowed to inline them -- the resulting bytecode doesn't have the farewell field at all! If you decompile the class (javap -c YourClassName), you'll see something like this:

public void sayGoodbyeInEnglish();
     0: getstatic     #2                  // Field
     3: ldc           #3                  //
String Bye bye
     5: invokevirtual #4                  //

The above corresponds to this line:


It's a bit daunting, but notice that "3:" line. The program isn't loading the value of the field farewell, it's loading constant #3 (which it note in a comment is the String "Bye bye") (you can see a list of the bytecodes on wikipedia).

Because farewell is a constant variable (and not a "real" static member), and thus can be inlined in the code, it doesn't matter where you define it -- the variable's lifecycle is essentially that of the whole JVM, not any one class or instance, and thus it can be declared anywhere.

Categories : Java

Related to : Why static final variables are accepted in local classes?
Is there any difference between normal and static local variables in static methods?
Yes both are different. For every call a will be created whereas b will be created only once and it is same for all the objects of type A. By same I mean, all the objects share a single memory of b.

Categories : C++
Php runkit_import and final classes
It's of... limited use. An illustration: final class Foo { protected $var = '456'; function doSomething(){ return '123'; } function getVar(){ return $this->var; } } class Bar { } runkit_class_adopt('Bar','Foo'); $d = new Bar(); var_dumP($d->doSomething()); //string(3) "123" var_dumP($d->getVar()); //PHP Notice: Undefined property: Bar::$var in ....

Categories : PHP
Static non-final variable used during initialization
The problem is that the variable : private static final Singleton SINGLETON_INSTANCE; might not have been initialized in that static block of yours, you can inititalize it to null(say) in the else clause inside that static block, i.e: static { Singleton tmp = singletonTMP; if (tmp != null) { SINGLETON_INSTANCE = tmp; } else {

Categories : Java
Update private static final int in base class for unittest
This may not be possible even using reflection, because the value of the field may be inlined by the compiler (I believe the compiler is free to choose whether it does this or not). In that case, a copy of the value is hardcoded where it is actually used, and can be changed only by manipulating the byte code. This is explained in more detail in this question: Change private static final field usi

Categories : Java
confusion over static final method accessed by multiple threads at the same time
Can 2 different threads can call a final static method at the same time? Yes , 2 different threads can call a final static method at the same time. Does a static final method become synchronized ? You can make the method synchronized explicitly but that will be class level synchronization not the object level. Yo need to use Synchronized keyword for this.

Categories : Java
Recently Add
Redirecting the output directory of 'mvn package' or 'mvn compile' command
No suitable constructor found for ProductoExtranjero
java Composite design pattern(Directory &File)
Java JTree's ui refresh after removing node from parent
First REST Spring application
How to cancel Indexing of a Solr document using Update Request Processor
PowerMock - Mock a Singleton with a Private Constructor
Calling a Postgres stored function SQL error
Where to store Morphlines Java custom command class?
Generic repository using map
How can I scroll a ScrolledComposited in Eclipse SWT Design view?
2 Frames/layout in 1 Activity
Writing a switch differently
Next button opens another activity when its reaches the array limit
Is EclipseLink MOXy capable of applying JSR-303 Bean Validation when unmarshalling XML to object?
Why my jdk can't work,and before the java_home, there is a space that is not from me
How to add List of objects in a Map
How to make notepad++ function like regular notepad in cmd?
Cell renderer and the lost focus
how can I implement iterable for LinkedList>
Disable Androids image-crunch in eclipse (run as) builds
java 8 lambda != myMap.size() after merging myMap
Issue with Calendar calculation that spans 2 calendar years
JSF 2.0 Spring bean injection
Java Regex ReplaceAll with grouping
Getting any word and last word using sed
Clicking on link on JEditorPane throws IOException
printing out difference of two arrays
Spring Bean Alias in JavaConfig
Using Factory Method to Create Generics
© Copyright 2017 Publishing Limited. All rights reserved.