Inner Classes - Quiz
Total: 13 questions
1. What is an inner class in java?
What is an inner class in java?
An inner class is a "class within a class" in java.
2. Types of inner classes in java.
Types of inner classes in java.
- Regular inner classes (or member inner classes)
- Method-local inner classes
- Anonymous inner classes
- Static nested classes
3. Where "regular" inner class can be declared?
4. Street is an inner class of the Town. Update the code to create a legal instance of Street class.
public static void main(String[] args) {
Town town = new Town();
??? street= ???;
street.seeTown();
}
Street is an inner class of the Town. Update the code to create a legal instance of Street class.
public static void main(String[] args) {
Town town = new Town();
??? street= ???;
street.seeTown();
}public static void main(String[] args) {
Town town = new Town();
Town.Street street = town.new Street();
street.seeTown();
}
5. What is the difference between instantiating a (non-static) inner class inside and outside of the outer class?
What is the difference between instantiating a (non-static) inner class inside and outside of the outer class?
- Inside an instance (non-static) method of the outer class, create it like any other object: Street street = new Street(); The current this becomes the enclosing instance.
- Inside a static method of the outer class (such as main) there is no this, so the enclosing instance must be supplied explicitly: town.new Street() or new Town().new Street(). The simple name Street is still in scope.
- Outside the outer class, the type name must be qualified with the outer class name: Town.Street street = town.new Street();
6. Update //1 and //2 to print correct references to inner(Street.class) and outer(Town.class) classes.
public class Town {
public String name = "Kharkiv";
class Street{
public void watchTown() {
System.out.println("Town name is " + name);
System.out.println("Street class ref is " + //1);
System.out.println("Town class ref is " + //2);
}
}
}
Update //1 and //2 to print correct references to inner(Street.class) and outer(Town.class) classes.
public class Town {
public String name = "Kharkiv";
class Street{
public void watchTown() {
System.out.println("Town name is " + name);
System.out.println("Street class ref is " + //1);
System.out.println("Town class ref is " + //2);
}
}
}public class Town {
public String name = "Kharkiv";
class Street{
public void watchTown() {
System.out.println("Town name is " + name);
System.out.println("Street class ref is " + this);
System.out.println("Town class ref is " + Town.this);
}
}
}
7. Where method-local inner class should be instantiated?
Where method-local inner class should be instantiated?
Within the same method (block), but after the class definition.
8. Where is the mistake?
2. Kangaroo kangaroo = new Kangaroo() {
3. public void movement() {
4. System.out.println("jump");
5. }
6. }
7. Tiger tiger = new Tiger();
Where is the mistake?
2. Kangaroo kangaroo = new Kangaroo() {
3. public void movement() {
4. System.out.println("jump");
5. }
6. }
7. Tiger tiger = new Tiger();Missing the semicolon in the line 6.
9. What is the difference between a static nested class and an inner class in Java?
What is the difference between a static nested class and an inner class in Java?
An inner class instance is always tied to an outer instance: it is created with outer.new Inner(), uses the outer object fields directly and keeps a hidden reference to it. A static nested class has no such link: it is created with new Outer.Nested() and directly sees only static members of the outer class (other members only through an explicit object reference). If a nested class does not need the outer instance, declare it static: this removes the hidden reference that can keep the outer object in memory.
10. Why can local and anonymous classes use only final or effectively final local variables?
Why can local and anonymous classes use only final or effectively final local variables?
A local or anonymous class object can outlive the method call (it may be returned or passed to another thread). So the compiler does not keep a reference to the variable itself; it copies its value into a hidden field of the class. If the variable could change, the copy and the original would disagree. Up to Java 7 the variable had to be explicitly declared final; since Java 8 it is enough for it to be effectively final, meaning its value never changes after initialization. Otherwise the compiler reports local variables referenced from an inner class must be final or effectively final.
11. Can an inner class declare static fields and methods? What does illegal static declaration in inner class mean?
Can an inner class declare static fields and methods? What does illegal static declaration in inner class mean?
Up to Java 15, inner classes (member, local and anonymous) could declare only static final compile-time constants; any other static field or method failed with illegal static declaration in inner class. Since Java 16 (JEP 395, delivered together with records) the restriction is gone: inner classes may declare static members, and you can declare local records, enums and interfaces inside a method. On older JDKs, move the static member to the outer class or make the nested class static.
12. When do you still need an anonymous class instead of a lambda expression?
When do you still need an anonymous class instead of a lambda expression?
A lambda can replace an anonymous class only for a functional interface (exactly one abstract method, such as Runnable or Comparator). You still need an anonymous class when:
- you must extend a class, including an abstract class, rather than implement an interface;
- the interface has more than one abstract method;
- you need your own fields (state) or helper methods;
- this must refer to the implementing object itself: in an anonymous class this is the anonymous object, while in a lambda it is the enclosing instance.
13. Can an outer class access private members of its inner class?
Can an outer class access private members of its inner class?
Yes. Access is symmetric: the outer class can read and write private fields and call private methods of its inner (or static nested) class through a reference to it, for example street.house = 78;, and the inner class can access private members of the outer class. The private modifier restricts access at the level of the top-level class, not the nested class.