Why do local inner classes not allow static variables ? The static fields of the classes could at least have been allowed access from the local method ?

Actually not 100% true. Static variables can be used if declared final]/i] which is good for the serialVersionUID.

I guess it is just because it would be to difficult (or time consuming at run time) to implement because the Java Virtual Machine has restrictions on calling private members from outside of their class, a special access method is generated by the compiler and added internally to the meth_inner class. This method has a name access$0(), and it in turns calls f(). In other words, a method is generated that can be called from outside of meth_inner, and grant access to a private method of meth_inner. So when f() is called above, a generated method access$0() is called, and it in turn calls f().

The static variables need not build the instance class.however,the inner class rely on the inner instance class.

Why do local inner classes not allow static variables ? The static fields of the classes could at least have been allowed access from the local method ?

Because static members make little sense when it comes to "local inner classes" since these classes belong to a scope as opposed to another class or a top level package. The "outer" world is not aware of this local inner class and always deals with instances of this class which are typically exposed via some sort of interface.

If there is any state that has to be maintained at the "local inner class" level, it can be easily done by doing the same at the enclosing method level. But probably the biggest reason might have to do with preventing the external entities from modifying the "class" level state of this inner class. Hence, only final primitives are allowed to be declared static when it comes to local inner classes.

class DamnIt {
    
    void damnIt() {
        class Test {
            static boolean bb = false;  // error; not final
            final static boolean b = false; // works
            final static Object o = new Object(); // error; not primitive
        }
    }    
    
}

Thanks everyone. I get that a local inner class' static variables won't be known outside the method, and so are definitely useless for them. As I understand it, local classes are primarily used to OOPify the working of a particular method that has little relation to other methods. But like we normally have static fields in a normal class (there are so many uses of them), why can't we benefit from them in their local class ? The local method code following the local class can be akin to main() that can make the object of the class and use it (along with its static variables) in whatever way.

What is meant by "maintaining the state at the local class level" ? If its the state of the objects, how does allowing non-final static fields interfere with this maintenance ?

If I understand it right, when u say "preventing the external entities from modifying the "class" level state of this inner class", u are giving the reason for making static variables final. (By class-level state, I understand the static variables). But modifying the static variables of a class may well be desired in some cases ?

But like we normally have static fields in a normal class (there are so many uses of them), why can't we benefit from them in their local class ?

Do you have any scenario in mind which can be handled properly *only* if local inner classes are allowed to have static members?

What is meant by "maintaining the state at the local class level" ? If its the state of the objects, how does allowing non-final static fields interfere with this maintenance ?

It does not interfere, it simply isn't required IMO.

But modifying the static variables of a class may well be desired in some cases ?

In *which* cases? Also, why would modifying the state of a class which should ideally be visible to the declaring method be desired?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.