0

Im trying to learn java, but I come across obsticle that Im already trying to understand for few days but I just don't get it I searched videos on yt checked on here but can't find right explanation that I will understand.

My question is what is point of inner classes and explanation what each type of inner classed is used for, like anonymous,local inner...

I heard some people saying it's good for encapsulation does that mean it's not that easy to access class, and also there stays namespace if I want to create normal class with same name? Also maybe if I need just single use of class in other class I just create inner then? and I should do normal classes just when I need it more then once? Generally I don't know when I should use it, I get syntax and everything I just don't see it's point, why should I use it instead just normal class.

renekton
  • 61
  • 1
  • 8
  • What does the [documentation](https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html) not cover? Or other questions on this site like [What are the purposes of inner classes](https://stackoverflow.com/questions/11398122/what-are-the-purposes-of-inner-classes)? – takendarkk Oct 06 '17 at 18:13

1 Answers1

0

This question depends a bit on personal opinion and preferences. I, personally, don't use local inner classes at all.

It all revolves around how you, the programmer, like to design your architecture. There are no direct pro or cons except accessibility.


I would say the point of anonymous classes is to quickly provide a simple implementation at a single location in your code. If you need to reuse the anonymous class or start to create a complex anonymous class I would recommend to create a regular class instead.

Something similar can be said to local inner classes. They can be useful if you have a big project, for example with multiple contributors, and you need a small auxiliary class in order to implement your current class. However you should use a regular class instead if the auxiliary class could be of use to others or if you would need it again later. But if that is not the case and it is purely an auxiliary class for that one class, you can indeed create it as local inner class.


A common used example for anonymous classes:

WindowAdapter adapter = new WindowAdapter() {
    @Override
    public void windowCloses(WindowEvent e) {
        doSomething();
    }
}

No need to create a new regular class if you just want to call a method at window shutdown.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Maybe do you have any program example of inner class use? – renekton Oct 06 '17 at 22:49
  • Nothing that common. Usually I see some small container/wrapper classes there. Just for the purpose to store some values. The Java API for example has the class `Entry` inside `Map`. Or `LinkedList` also has an `Entry` class. It also has some special `Iterator` classes inside. But in that case I would rather create a package for `LinkedList` and store everything as regular class there. But I think they wanted to keep everything compact and clear, thus `java.util` and then directly all classes without subpackages. – Zabuzard Oct 07 '17 at 10:10