Non access modifiers for Inner class variables

S

shila

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
 
H

hiwa

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.
 
J

John C. Bollinger

hiwa said:
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
(e-mail address removed)
 
T

Tor Iver Wilhelmsen

John C. Bollinger said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top