![]() |
|
|
|||||||
![]() |
Java - Non access modifiers for Inner class variables |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
I am preparing for my scjp exam, and cud not find specific answer to
my question that why Inner Classes cannot declare static variables. If anyone cud provide some info on this, it'wud be very helpful. thanks in advance shila |
|
|
|
|
#2 |
|
Posts: n/a
|
(shila) wrote in message news:<. com>...
> I am preparing for my scjp exam, and cud not find specific answer to > my question that why Inner Classes cannot declare static variables. If > anyone cud provide some info on this, it'wud be very helpful. > > thanks in advance Inner class is a non-static nested class inside another class. That means its existence depends on enclosing class's instance/object. In other words, they can not have their own independent existence as a sovereign class ... if enclosing class object is not created, inner class can't have its existence, so they can't have static members. Only an independent sovereign class can have its own static members. |
|
|
|
#3 |
|
Posts: n/a
|
hiwa wrote:
> (shila) wrote in message news:<. com>... > >>I am preparing for my scjp exam, and cud not find specific answer to >>my question that why Inner Classes cannot declare static variables. If >>anyone cud provide some info on this, it'wud be very helpful. >> >>thanks in advance > > Inner class is a non-static nested class inside another class. That > means its existence depends on enclosing class's instance/object. In > other words, they can not have their own independent existence as a > sovereign class ... if enclosing class object is not created, inner > class can't have its existence, so they can't have static members. > Only an independent sovereign class can have its own static members. And although the stated facts are all true, they do not constitute an insurmountable problem. The fundamental answer is that Sun decided to place the restriction for reasons of their own, even though the virtual machine specification is not incompatible with inner classes having static members. I don't work for Sun and never have, so I can only speculate as to Sun's reasons, but I would place my bets in two areas: (1) A philosophical objection based on arguments similar to those hiwa gave above, and (2) Reserving the possibility of a future JVM revision that may be facilitated by the restriction. John Bollinger |
|
|
|
#4 |
|
Posts: n/a
|
"John C. Bollinger" <> writes:
> even though the > virtual machine specification is not incompatible with inner classes > having static members. Mostly because nested classes are synthesized into top-level classes with compiler-enforced restrictions on use. > (2) Reserving the possibility of a future JVM revision that may be > facilitated by the restriction. .... like turning anonymous inner classes into proper lambda expressions or Smalltalk-like blocks. |
|