Class internal implementation

The inner class is called a member of the outer class. Does this mean that whenever an object of an outer class is created, an instance of the inner class is also created implicitly?

+6
source share
3 answers

Not. An instance of the inner class is created only when it is created.

Note that the constructor of the inner class requires an instance of the outer class (although this is masked by the compiler). This is true for non-static nested classes. Static nested classes can be created without a parent instance (since they are static)

+8
source

There are different types of inner classes, each of which acts differently.

  • Static Member Classes
  • Class classes
  • Local classes
  • Anonymous classes

You can find a good overview of all of them here: http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html

+3
source

This means that you need an instance of the outer class before you can have an instance of the inner class

0
source

Source: https://habr.com/ru/post/891544/


All Articles