• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner classes and static members

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone !



We know that it is not allowed. My question is - why ?
Is it because nested classes are tied to outer classes,
so we could actually think of nested classes as "separate"
or "different" classes for each different instance of the
outer class ? What I mean is if we have two different outer
objects, then we could follow two logics here:

Outer o1; Outer o2;

1) Outer.Inner.x=10; => o1.Inner.x==o2.Inner.x

2) or perhaps Outer.Inner.x should be not allowed
and the only way to access x would be o1.Inner.x
and o2.Inner.x and o1.Inner.x should be != o2.Inner.x ?

The first one seems to be easier to understand and potentially
to implement it (in the javac and/or JVM). But since there is already
that kind of semantics (static inner classes), and this is
that kind of inner class which is binded to outer class,
the second one might be the "better" attempt. And that
could possibly cause a lot of troubles in implementing
it as a new java feature and finally would be the answer to
my question why it is not allowed.

Please note, that it is allowed to have a static member
field which is declared final - why ?

However - which is even more strange to me - it is allowed
to define a nested class which extends class which has
static members, so implicitly the new defined nested class
HAS static members ...

Could anybody put some light on that topic please ?



And as a bonus - why inner classes defined inside
interfaces are implicitly static ?


Cheers,

Adrian
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adrian Sosialuk:
And as a bonus - why inner classes defined inside
interfaces are implicitly static ?



This one is simple: we need an instance of the outer class for being able to instantiate an inner class. The problem with interfaces is that it doesn't matter how hard we try, we'll never be able to instantiate an interface. So, it looks right that inner classes inside an interface are implicitly static.
 
Adrian Sosialuk
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sergio,

Thanks for your quick reply.

Yes - I agree and I was aware of that
I was rather thinking why compiler doesn't allow
to make the "inside the interface" class nested
in the implementing class ... So if someone writes
a class implementing that interface, the class from
that interface COULD become nested. Unless someone
explicitly marks the class as static.

I know - I'm asking for problems but this way
I can learn about the real underlying concepts
about the java


Cheers,

Adrian
reply
    Bookmark Topic Watch Topic
  • New Topic