• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inner classes query  RSS feed

 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am learning about inner classes but I have a nagging query and it's this...

When you create an instance of the outer class you obviously do not automatically create an instance of the inner class. You have to do it explicitly somewhere. I've been shown two ways of doing this
1). within the outer class itself, within some method.
2). or you can do it outside the class.

Is one method more common (even better) than the other? And what does best practice dictate?
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm speaking about non-static classes here not static.
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a bit like saying: "Is it better practice to do a+b or a*b?" You use whatever you need at that time.

Having said that, if you find that you have to create an instance of the inner class outside of the outer class, it's an indication that your design is wrong.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I presume you have read the Java™ Tutorials about nested classes. Also search for Thinking in Java by Bruce Eckel; it used to be possible to find a copy of the 3rd edition free of charge. Eckel has a good section about inner classes. Also find out from that Java™ Tutorials link how one can replace many inner classes by λs in Java8.
You will see that inner classes shou‍ld always be instance members of their surrounding type, so they only exist as part of an instance, just as an instance field only exists as part of an instance. You will also have seen that you call nested classes something different if they are static. You can therefore instantiate a public static nested class easily:-
new Outer.Inner(...)
...but you need an instance to instantiate a public inner class:-
myOuterObject.new Inner(...)
...or even
new Outer(...).new Inner(...)
That problem does not occur at all if the inner class has private access; simply write
new Inner(...)
somewhere inside the class.

If you read Eckel you will come up with a simple rule of thumb à la George Orwell:- public static nested class good, public inner class bad.
 
Fergus Flanagan
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The last few days I've covered static/non-static inner classes, anonymous classes and abstract classes and tbh everything is feeling abstract right now. I have a feeling I'll only truly understand this stuff when i've been coding properly for a fair bit. Now it's all just theory so forgive me if I'm a bit woolly in my understanding.


I presume you have read the Java™ Tutorials about nested classes.


Found it very dry so chose to do online video tutorials. But will have to start referring to it eventually I know but just easing into it at the moment.

Thinking in Java by Bruce Eckel;

[Moderation comment] Link has been removed

Incidentally is this still a worthwhile read considering we are on Java8?

You will see that inner classes shou‍ld always be instance members of their surrounding type,

Well that's the text book definition of what an inner class is, right?

You will see that inner classes shou‍ld always be instance members of their surrounding type, so they only exist as part of an instance,

May be I not understanding what you are saying here. But are you saying an inner class can be instantiated within the class it's been declared in or equally outside of the class it's been declared in since in either case it's existing only because the outer class instance exists?
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You cannot find the inner class until an instance of the outer class exists. That does not apply to static nested classes. I think trying some examples will help you familiarise yourself with nested classes. Remember they are not a major feature of Java® however.
Yes, Eckel is still worth reading. If you find an older edition, you can see how he has changed his tune about Exceptions. I am not convinced I agree with him about Exceptions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!