83

If I have for example a class along with a helper class to do some of its functionality, does it make sense to make it as an inner class.

    public class Foo {
       private FooHelper helper;

       // constructor & any other logic

       public void doSomeThing() {
         helper.do();
       }
    }

    public class FooHelper {
        public void do() {
         // code
        }
    }

In the above case does it make sense to make the FooHelper as an inner class ? Apology if this sound stupid but I am little confused about the use cases.

7
  • 1
    Generally for actionlisteners and when you do not want to expose your call to others. Aug 23, 2013 at 6:19
  • @Vaibhav Jain thanks for the link, I am aware of the syntax, but m bit confused about when to use it and if there is any trade off
    – Hild
    Aug 23, 2013 at 6:22
  • @SayemAhmed I am not sure if that is the only option available, It seems there could be multiple alternative and being a newbie m not sure about the proper approach, so still digging
    – Hild
    Aug 23, 2013 at 10:13
  • 2
    @AnilSharma: Think about this: "Why expose a thing to outer world to which the thing has nothing to do". By this I mean: if you need a functionality (the class and its methods) that is needed for one single class and outer classes don't need it or its functionality, its better not to expose this functionality to other classes. This way you avoid polluting the outer world, which leads to better architecture. Jan 7, 2015 at 9:07

10 Answers 10

95

Yes, it makes perfect sense to make it an inner class. If no other classes need it, make it private. If it doesn't require exclusive access to the members of the outer class, make it a static nested class because then it will require less memory space.

Check out the recommendation from the official tutorial -

Use a non-static nested class (or inner class) if you require access to an enclosing instance's non-public fields and methods. Use a static nested class if you don't require this access.

7
  • 1
    And when should it be a static inner class ? if there any difference from the functional point of view?
    – Hild
    Aug 23, 2013 at 6:23
  • 4
    @Hild: A static nested class doesn't have access to the members of the enclosing class (unless it is static). If this matches your requirement, make it static. Non-static inner classes, on the other hand, have full access to the members of the enclosing class. Aug 23, 2013 at 6:26
  • @Hild static nested classes do not contain synthetic references to their enclosing class. So unless you have specific reasons to make it non-static always strive to make it a static nested class. Static inner class is a technically wrong terminology.
    – Geek
    Aug 23, 2013 at 7:26
  • thanks was really useful, but @eddieferetro has a valid point why not uses private method instead
    – Hild
    Aug 23, 2013 at 10:11
  • Why does static nested class require less memory space?
    – getsadzeg
    Apr 23, 2019 at 11:36
25

From JAVA SE Docs

Why Use Nested Classes?

It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

So Yes, it makes sense to use FooHelper as an inner class.

22

If you think that FooHelper will not at all be useful for other classes than Foo, then it makes sense to make it as private inner class of Foo. One example of this kind of design can be found in HashMap where it defines a private inner class KeySet

Otherwise having it as a private instance looks good.

9

Here are some uses of inner classes.

  • Inner classes are used to get functionality which can get an object better than method.
  • They can be used in the case when a set of multiple operations are required and chances of reusability are good inside the class and they will not be accessed but methods outside the outer class.
  • Inner classes are made to achieve multiple inheritance also.
  • Inner classes are used when they are useful in class context.
  • They are used to separate logic inside classes.

So if you have some requirement matching above points than inner classes can be used. It is always better to make inner class private to prevent access from other classes. In your case use of inner classes is helpful to make code readable and separate logic in the outer class.

3

As per Oracle Docs, Simply Explained

Compelling reasons for using nested classes include the following:

It is a way of logically grouping classes that are only used in one place: If a class is useful to only one other class, then it is logical to embed it in that class and keep the two together. Nesting such "helper classes" makes their package more streamlined.

It increases encapsulation: Consider two top-level classes, A and B, where B needs access to members of A that would otherwise be declared private. By hiding class B within class A, A's members can be declared private and B can access them. In addition, B itself can be hidden from the outside world.

It can lead to more readable and maintainable code: Nesting small classes within top-level classes places the code closer to where it is used.

2

Inner classes make sense when they are tiny and don't need names. Listeners in GUIs are classic examples where they make sense.

If the class is big and important, it should be named and placed in a separate file.

The listener classes in normal GUI examples do one tiny thing, usually just dispatch to some other function to do real work.

I also often use static nested classes (which are technically not inner classes) for classes which are only used in the context of another class - Map.Entry is a good example of this. It's only used in conjunction with a Map, so having the definition of Entry be a part of the Map interface makes organizational sense.

I don't generally have much use for other types of nested classes, like nonstatic member classes and local classes. But they do occasionally come in useful. For a good example of a legitimate use for member classes, see the source code for LinkedList.ListItr. This is a private inner class whose purpose is to provide an implementation of ListIterator for a LinkedList. To do this, it's useful to have access to the private data inside the LinkedList. To achieve this using only top-level classes, it would have been necessary to expose more public methods in LinkedList to allow the ListIterator to get at the underlying implementation of the LinkedList. Instead, using an inner class allows LinkedList to keep its implementation private, as it should be.

7
  • Sorry, im an eternal newbie and would like an explanation because I cannot see the place of innerclases in the OOP world: They can access to private members of outer class... You can't reuse them, only by copy-pasting... Make the code ugly and more hard to read and mantain... (continues)
    – inigoD
    Aug 23, 2013 at 7:21
  • I only understand your argument of not to expose a lot of public methods when optimizing an app, but cannot see what is it sense in GUIs: Listener pattern gives you the option of change an item behavior (what to do in an button 'onclick' event, for example) in execution time. If the listener is an inner class it usually indicates that you, really, didn't need listener capabilities: you wanted an 'onClick' method that you will never reuse (like occurs usually in android)
    – inigoD
    Aug 23, 2013 at 7:22
  • @eddieferetro one use cases I can think of is not exposing an entire object when it is to be used in a specific class.
    – Hild
    Aug 23, 2013 at 7:45
  • 2
    @hild but, if its an especific object for an specific class why not create private methods in outer class withouth create a new innerclass?.
    – inigoD
    Aug 23, 2013 at 8:05
  • @eddieferetro The inner class is just a way to cleanly separate some functionality that really belongs to the original outer class. Some piece of functionality in your outer class would be most clear if it was implemented in a separate class.Even though it's in a separate class, the functionality is very closely tied to way that the outer class works.
    – code_fish
    Aug 23, 2013 at 8:18
1

Yes, the advantage of using inner class is it can access members of outer class.In your case , if you think your FooHelper is not to be used by any other class,you can make it a inner class.

To check out the utility of inner class, go through the examples of AWT. Anonymous inner classes are widely used in event handlers.

0

What is Foo's scope? When Foo is domain model class with it's own lifecycle and helper is common service, seems like mixing of two objects with very different scope/lifecycle.

Typically domain entity has it's own lifecycle from creation, persistence to it's GC. On the other hand, helper or service is either static or better dynamic with lifecycle equals to the whole app, e.g. spring bean.

Once your domain entity would contain reference of a service it can bring you serious problems. E.g. every call to repository's Get needs to inject reference of this service into domain entity. I'd recommend to avoid this pattern.

It's not apparent for me who will make instance of Helper for you.

0

Hild, Yes, it makes sense to use an inner class in many cases.

Think of it this way - the inner class will live and die with the outer class, so any functionality that is specifically needed for the outer class can be added to the inner class. Popular examples are - Listeners in most cases - Types of KeyListeners, MouseListeners, eventListeners.

Classes in Java allow you to have specific functionality, but sometimes you may need to have a separate specialized functionality but it also needs to be intimately tied to the class you're designing.

There can be four types of inner classes. A simple google search can help you to find out more about them.

0

Nested Classes,enable you to logically group classes that are only used in one place, increase the use of encapsulation, and create more readable and maintainable code. Local classes, anonymous classes.

http://docs.oracle.com/javase/tutorial/java/javaOO/whentouse.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.