Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inner class instantiation

An inner class is said to be a member of the outer class. Does that mean that whenever an object of the outer class is created, an instance of inner class is also created implicitly?

like image 686
aps Avatar asked Jun 28 '11 11:06

aps


People also ask

Can static inner class be instantiated?

A static inner class can be instantiated without the need for an instance of the outer class. In general, an Inner class is a part of nested class, called Non-static nested classes in Java. The types of inner classes are member inner class, anonymous inner class, and local inner class.

What is an inner class in Java?

In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is purely object-oriented so bringing it closer to the real world.

Can inner class be serializable?

Because inner classes cannot declare static members other than compile-time constant fields, they cannot use the serialPersistentFields mechanism to designate serializable fields.

Do inner classes need static?

Inner classes are non-static member classes, local classes, or anonymous classes. In this tutorial you'll learn how to work with static member classes and the three types of inner classes in your Java code.


1 Answers

No. An instance of the inner class is created only when you instantiate it.

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 instantiated without a parent instance (since they are static)

like image 81
Bozho Avatar answered Oct 13 '22 18:10

Bozho