Reputation: 20391
In java, you can have inner classes. I'm curious from a design perspective if there are any benefits to this.
My initial thoughts are that having a separate file with the class is cleaner in separating things into reusable modules. By doing so, if other classes wish to use that other class, they can also make their own instances. To me it seems like avoiding the inner classes may be a better design for extensibility and code reuse, and when working on new projects, its normally hard to tell if the class would be reused or not... so I feel siding on having the separate classes is the way to go.
I ask this question because I took over a project which has a bunch of these... which makes me think the previous developer may of just been lazy or unfamiliar with the IDE. But I want to make sure I'm not missing any benefits that these inner classes have. If there are benefits can someone let me know, and then I can confirm if the previous developer was taking advantage of these benefits.
Upvotes: 16
Views: 13235
Reputation: 2790
Inner classes are useful to:
Hide implementation details.
Make project view more concise and thus understandable.
Add logical grouping of the related code.
Upvotes: 3
Reputation: 19260
Non-static inner class:
1) Inner class will be used to perform some task over the outer class and returns the output
. So it will ensure that the outer class object is created to access it.
2) Inner class is only specific to the outer class and no use on the outside world. So no need to think about code reuse.
e.g_1) The best example for inner class is iterator
implementation of collection framework. Set(AbstractCollection.java) and List(AbstractList.java) will use different iterator inner class implementation.
-> We can reuse the inner class code. For example implementation of private class Itr
innerclass at java.util.AbstractList
is used by both Arraylist and Vector.
Static inner class:
1) Static inner class won't depend on a outer class object; but it is specific to outer class level. It wont be exposed to standalone since it is specific to the outer class only. So no code reuse.
2) It has group of methods which is common(shared) to all the objects (Class level)
3)
e.g_1) Sigleton object retrival using static inner class.
e.g_2) Builder Pattern
e.g_3) Comparator implementation for a class .
Upvotes: 2
Reputation: 15230
One good usage of inner classes that comes into my mind is in java.util.ArrayList that hides its iterators implementations into private inner classes
. You can't create them except by invoking iterator()
or listIterator()
on the list object.
This way the Iterator
and ListIterator
implementations for ArrayList
are grouped with their related class and methods for enhanced readability (the implementations are pretty short), but hidden from others.
They can't be declared static
as they need access to their enclosing instance object.
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable {
...
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
...
}
public ListIterator<E> listIterator() {
return new ListItr(0);
}
private class ListItr extends Itr implements ListIterator<E> {
...
}
}
Upvotes: 3
Reputation: 424983
Inner classes are good when they are not used outside the containing class - it's a way of curtailing class bloat. As a general rule, that's the only time I use them.
Non-static inner classes also have access to the containing instance's private fields.
Upvotes: 18