It's not a secret anymore!*
The moose likes Cattle Drive and the fly likes Inner Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Inner Classes" Watch "Inner Classes" New topic
Author

Inner Classes

Mark Gandy
Greenhorn

Joined: Jul 08, 2008
Posts: 10
I recently received a nitpick telling me not to declare a class within a method.
Can anyone tell me why I shouldn't be using method-local inner classes?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Just off the top of my head, I would say that method local inner classes tend to be less simple than .... some other options. Can you find a simpler way to accomplish your goal?


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Mark Gandy
Greenhorn

Joined: Jul 08, 2008
Posts: 10
Ok, for some background, but hopefully without giving too much away...

I'm on SortNames (OOP3). I need to declare a class that implements a certain other class so that I can do a sort. I initially declared the class as its own outer class, but was told it would be better as an inner class (I'm not sure why).

So, thinking I should declare the class right before it is used, I declared it within my main method before it is instantiated and I do the sort. I was then told that, as a general rule, it's not a good idea to declare a class within a method.

So now, the only option I can see left is to declare the inner class as a "member" of the outer class. But then to instantiate the inner class I would have to instantiate the outer class, and I'm struggling to see how that would be a better approach?

Hope this all makes sense!
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Can you can think of a way to declare the new class as a "member" of the outer class without needing to instantiate it in order to use it?
Mark Gandy
Greenhorn

Joined: Jul 08, 2008
Posts: 10
You mean declare it as static? But I need an instance of the inner class in order to do the sort. At least I think I do...
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20497
    ∞

Have you read this? http://www.javaranch.com/campfire/StoryInner.jsp


permaculture Wood Burning Stoves 2.0 - 4-DVD set
Mark Gandy
Greenhorn

Joined: Jul 08, 2008
Posts: 10
Aaahh! I CAN instantiate a static inner class.
You can discard my latest attempt - I'll submit a revised version shortly
Thanks Paul - I'll have to look through the rest of those campfire stories...
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1194
There's no replacement for learning by doing. Here's another perfect example! Long live the Cattle Drive!
David Sharpe
Ranch Hand

Joined: Jun 15, 2009
Posts: 32
http://www.javaranch.com/campfire/StoryInner.jsp wrote:A top-level nested class is little more than another way to control namespace.


True, and therefore they are mostly unwanted since packages give us this control.

I think the point is missed in explaining how it's done and not why. That campfire article does a good job of explaining some pros and cons of inner classes, but it really only brings up "top-level nested classes" (i.e. "static inner classes") to say that "inner classes aren't these" (I'm not suggesting that it should elaborate any more than it already does).

Mark Gandy wrote:I initially declared the class as its own outer class, but was told it would be better as an inner class (I'm not sure why).

Not much point in doing it if you don't know why.

I'm a beginner, but the only time I've ever used a static inner class is when it was also private. For example:

It says "rubbish to reuse; I'm information hiding," and it's probably not a very good technique. In other words, I've used static inner classes for encapsulation, and I propose that's a potential use in addition to controlling namespace. I welcome correction.

In your case, I suspect a static inner class is being recommended for namespace control. If the class in question is called something generic like "Sorter", it's possible that it already exists in your package. In that case, a static inner class might be preferable to creating a new package (but it also might indicate that your package hierarchy isn't robust enough).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner Classes
 
Similar Threads
Traps to be aware of in any SCJP test !!!!! :)
classes -No one is giving answers
Inner classes
Inner classes
Inner Classes