Java Interview Questions - Nested class

1. What is Nested top-level class?

A top level class does not appear inside another class or interface. If a type is not top level it is nested.

2. What is the difference between inner class and nested class?

Both static and non static nested class or Inner needs to declare inside enclosing class in Java and that’s why they are collectively known as nested classes but they have couple of differences as shown below:

  1. First and most important difference between Inner class and nested static class is that Inner class require instance of outer class for initialization and they are always associated with instance of enclosing class. On the other hand nested static class is not associated with any instance of enclosing class.
  2. Another difference between Inner class and nested static class is that later uses static keyword in their class declaration, which means they are static member of class and can be accessed like any other static member of class.
  3. Nested static class can be imported using static import in Java.
  4. One last difference between Inner class and nested static class is that later is more convenient and should be preferred over Inner class while declaring member classes.

3. What is anonymous class?

In short we can say that an anonymous class is an inner class that does not have a name at all. And whose instance is being created at the time of its creation.

Can you have an inner class inside a method and what variables can you access?

It’s possible if these variables are final.

5. What are the various uses of inner classes?

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, and lambda expressions also impart these advantages; however, they are intended to be used for more specific situations.

6. What is an inner class?

Inner class is a class defined inside other class and act like a member of the enclosing class.

7. What are the different types of inner classes?

There are two main types of inner classes.

  1. Static member class
  2. Inner class

8. What is static member class?

A static member class behaves much like an ordinary top-level class, except that it can access the static members of the class that contains it. The static nested class can be accessed as the other static members of the enclosing class without having an instance of the outer class. The static class can contain non-static and static members and methods.

9. What are non static inner classes?

A non-static inner class can be instantiated only inside a non-static method of the outer class.

10. Can a static nested class have access to the enclosing class non-static methods or instance variables?

No

11. What are the advantages of Inner classes?

There are many advantages, though some of them are really personal or design preference.

  1. with inner classes you can access private members of the enclosing class.
  2. they are useful for interface implementations that are only used by the enclosing class (event handlers in a application)
  3. they are useful for providing fine grained access and creation control over an interface implementation that is retrieved externally (maybe something like an Iterator implementation)

12. What are disadvantages of using inner classes?

Using inner class increases the total number of classes being used by the application. For all the classes created by JVM and loaded in the memory, JVM has to perform some tasks like creating the object of type class. JVM may have to perform some routine tasks for these extra classes created which may result slower performance if the application is using more number of inner classes.

13. What are different types of anonymous classes?

There are three types of anonymous classes present in java.

  1. Plain old anonymous class type one
  2. Plain old anonymous class type two
  3. Argument defined anonymous class

14. If you compile a file containing inner class how many .class files are created and what are all of them accessible in usual way?

If a inner class enclosed with an outer class is compiled then one .class file for each inner class and a .class file for the outer class is created.

15. How to access the inner class from code within the outer class?

The inner class is instantiated only through the outer class instance.