As per JLS §8.1.3 why does Java not allow static context in Inner class ?
Asked
Active
Viewed 142 times
0
-
Doesn't that say that static will result in compile time error. Is that enough of a reason? And related - https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html – OneCricketeer Jan 25 '17 at 05:19
-
1I want to know the reason . It will not compile is not the reason for that . My question is that why it will not compile ? – optional Jan 25 '17 at 05:20
-
Are you only asking *why*, or is the code you are writing breaking this rule and you want to know alternatives? – OneCricketeer Jan 25 '17 at 05:23
-
2Because it's not clear at which level a static member of an inner class should be shared. Logically, it would need to be shared with all the inner class instances belonging to the same outer class instance, but it's not possible to implement that easily/efficiently. – Erwin Bolwidt Jan 25 '17 at 05:23
-
@cricket_007 : I am not asking alternatives . I am interested in why Java made such kind of restriction. – optional Jan 25 '17 at 05:26
-
@ErwinBolwidt : I got your point but please add more clarification. – optional Jan 25 '17 at 05:27
-
Possible duplicate: http://stackoverflow.com/questions/25458617/why-cant-an-inner-class-use-static-initializer – Jan 25 '17 at 06:19
1 Answers
0
Inner class will belong to instance of its Outer class.
If Java accept static non final variables in inner class, then there may be different values for that static variables for different instances of outer class.
Its totally against the static context definition.

Anil Agrawal
- 2,748
- 1
- 24
- 31