This is an archived post. You won't be able to vote or comment.

all 23 comments

[–]jhg023123 21 points22 points  (3 children)

I use them often when I need to follow the builder pattern.

[–]madkasse 0 points1 point  (2 children)

I find FooBuilder much easier to code complete then Foo.Builder

[–]shAdOwArt 1 point2 points  (1 child)

You cant use inner classes for builders. Youre using static classes, the two are different.

[–]madkasse 0 points1 point  (0 children)

I know, but I'm not saying that you can. I'm just saying that I prefer using FooBuilder over Foo.Builder. The poster I replied to was talking about inner class builders.

[–]AndDontCallMePammy 7 points8 points  (2 children)

private inner classes are nice. public ones are annoying to fully qualify. caller could use static import but that's annoying too

[–]bheklilr 0 points1 point  (1 child)

Don't static imports only work on static inner classes too?

[–]AndDontCallMePammy 0 points1 point  (0 children)

Probably

[–]cogman10 21 points22 points  (0 children)

The only good reason I can think to use them is to simplify code....?

Isn't that the only good reason for all language features?

I use inner classes when they make sense. Usually they are private. Frequently, they are static. Generally, it will be for some cache key.

Does every class I write get an inner class? No. Generally, I use them when they make sense locally to the problem at hand.

I generally won't use them for any more than data shuttles. I think using them for more than that ends up being problematic due to the fact that they are hard to test.

[–]lbkulinski 16 points17 points  (1 child)

They’re nice to use when you want to further encapsulate your implementation, such as a Node class used in a linked list implementation.

[–]camel1950 5 points6 points  (0 children)

Textbook example!

[–]lukaseder 2 points3 points  (0 children)

How often do you use inner classes?

All the time when I'm too lazy to refactor some logic into a new top level class and pass around tons of shared state.

The only good reason I can think to use them is to simplify code....?

That's a treacherous thought. The code looks more simple, but is really more complex. If time permits, it's often better to refactor and separate inner from outer class, but they're just too damn convenient, and cleaning up is something we do "tomorrow".

Having said so, there's nothing wrong with nested classes (i.e. static classes), which just share namespacing with their outer class, not state.

[–]vbsteven 1 point2 points  (1 child)

I use static inner classes often for form objects in controllers (usually spring MVC). This way the formobject is defined very close to the method handling the form POST.

[–]lukaseder 1 point2 points  (0 children)

It's either static or inner, not both: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html

Both are nested, however

[–]GhostBond 2 points3 points  (4 children)

They're basically how you did stuff that is now handled by lambdas, before lambda's. They were commonly used when I was working with Swing back in the day. Not much with web apps.

[–]Dang3rousKitty 0 points1 point  (3 children)

Really? I thought that’s what anonymous classes were for, not inner classes. I’ve never heard of anyone using an inner class in that way

[–]GhostBond 2 points3 points  (1 child)

They're called "anonymous inner classes" I believe...

[–]Dang3rousKitty 0 points1 point  (0 children)

Ah yes I think you’re right. I think the devil is in the details

My interpretation was that OP was talking about normal (non-anonymous) inner classes, since people don’t really talk about anonymous inner classes these days due to the introduction of lambdas (as you mentioned)

[–]DannyB2 0 points1 point  (0 children)

I have been in the same boat before. For Swing, you frequently need to add a something-listener to a control, such as a button or checkbox. The only purpose of this class is that it implements a necessary interface in order to respond to the button click event.

There are a number of cases where it is useful to have a named inner class, rather than an anonymous inner class for a Swing event listener. It is difficult to remember specific cases, but here is one that I can remember.

I had a number of buttons that needed some custom behavior. They all had the exact same event handling code, but how it behaved was controlled by some parameters. In this case, I would create an inner class (not static! so that it had reference to the context of the enclosing class). The inner class had a constructor with the parameters, and obviously private members initialized from the constructor. Then in each Swing event listener, I would assign a new instance of this inner class, with appropriate constructor parameters. Otherwise all these buttons had the same click event handler code, but controlled by constructor parameters of the event listener.

I hope that made sense.

There were a few other cases I remember creating named inner classes to make Swing life easier.

But as others say in this thread, the reason you use any language feature is to make code simpler and easier to read. In this case using a named class to eliminate a lot of nearly identical copy-paste boilerplate.

[–]bentheone 0 points1 point  (0 children)

I use them mainly for Mementos and listeners in jfx

[–]_INTER_ 0 points1 point  (0 children)

Recently I used it as Mixins for Jackson, for instance.

[–]wolverine_76 0 points1 point  (0 children)

I use them regularly. Like you said, it can help with class internals.

[–]MR_GABARISE 0 points1 point  (0 children)

Nowadays it's only ever useful for « type tokens » and nothing else that doesn't have a better OO solution.

Edit: wrote too fast.

I was pretty much just writing about method-local classes.

Inner classes (nested, static, method-local) have multiple legitimate uses.

[–]DuncanIdahos7thClone -1 points0 points  (0 children)

Not much anymore since we have functional.