Classes/Objects  «Prev  Next»


Lesson 9Local Inner classes
Objective Using this with non-static inner classes

Java Local Inner Classes in Java SE 17

Local Inner Classes in Java SE 17, like in earlier versions, are a type of inner class that is defined within a method, constructor, or block of code. Their purpose is to logically group classes that are only used in one place, increasing encapsulation and making code easier to read and maintain. They are particularly useful when you need to create a class that will not be used outside of the method where it is defined.
Key characteristics of Local Inner Classes:
  1. Scope: The scope of a local inner class is restricted to the block of code in which it is defined (typically a method). This means it cannot be instantiated outside of this block.
  2. Access to Variables: Local inner classes can access final or effectively final variables from the enclosing block of code. This allows them to interact with the surrounding context in a controlled manner.
  3. Encapsulation: By keeping the inner class local to a method, you reduce its visibility to the rest of the program, encapsulating its functionality.

Example of a Local Inner Class:
public class OuterClass {
 void someMethod() {
  // Local variable inside the method
  final int number = 10;
  // Defining a local inner class inside the method
   class LocalInnerClass {
    void printNumber() {
     System.out.println("Number: " + number);
    }
   }
  // Instantiating and using the local inner class
  LocalInnerClass inner = new LocalInnerClass();
  inner.printNumber();
 }
 public static void main(String[] args) {
  OuterClass outer = new OuterClass();
   outer.someMethod();
  }
}


Explanation:
  • The `LocalInnerClass` is defined within the `someMethod()` method of the `OuterClass`.
  • It has access to the local variable `number`, which is `final` (or effectively final).
  • The `LocalInnerClass` is instantiated and used within the same method where it is defined, and it prints the value of `number`.

This approach is useful when the class is only relevant within the scope of a single method, helping to keep the code clean and organized by not exposing the class outside of its intended context.


Cover local inner classes and the use of this with inner classes.

The keyword this refers to the current object instance. When using non-static inner classes, there is a this associated with the inner class and with its enclosing class. When this is used within the scope[1] of the inner class, this refers to the instance of the inner class. When this is used outside the scope of the inner class, but within the scope of the outer class, this refers to the outer class. However, this can also be used to access the instance of the enclosing scope (outer class). The notation Outer.this can be used within Inner to reference the this instance of Outer. The InnerThis program provides an example of using this within inner classes.
InnerThis program
The InnerThis program illustrates the use of this in inner classes.
It creates an instance of the InnerThis class, which creates an instance of the Inner class.
The Inner class is an inner class of InnerThis.
The this.i expression refers to the i variable of Inner. The InnerThis.this.i expression refers to the i variable of InnerThis.
The output of the program is:


2
1

The InnerThis program
class InnerThis{
  int i = 1; 
  public static void main(String[] args){
    new InnerThis(); 
  }
  InnerThis(){
    new Inner(); 
  }
  class Inner {
   int i = 2;
   Inner(){
     System.out.println(this.i);     
     System.out.println(InnerThis.this.i);  
   }
  }  
}


Creating Instances of non-static inner Classes

When an instance of a non-static inner class is created, it is created with respect to its enclosing instance. This enclosing instance must exist before the instance of the inner class can be created. When this refers to the enclosing instance, the constructor of the inner class may be invoked directly (i.e., new Inner()). The notation new Outer.Inner() may also be used in cases where it is not clear that an instance of the inner class is being constructed.
  • Local inner classes: Local inner classes[2] are inner classes that are declared local to a block of code (e.g., local to a method). Local inner classes are not tied to an enclosing scope. Instead, they are treated as local variables. As such, they may not be declared as private, public, protected, or static. Local inner classes may only access the local variables or method parameters of the code block in which they are defined. Furthermore, these variables or parameters must be declared as final and assigned a value before being used.
    The LocalInner program illustrates the use of local inner classes.


Local Inner Program in Java
The LocalInner program is a minimal window program. The LocalInner constructor declares a local inner class named WindowHandler, which is used to handle the WindowEvent that is generated when the user attempts to close the window.
The WindowHandler class is a small class that is created and used within the scope that it is needed.

import java.awt.*;
importjava.awt.event.*;
class LocalInner extends Frame {
public static void main(String[] args) {
  new LocalInner();
 }
  LocalInner() {
    class WindowHandler extends WindowAdapter {
      public void windowClosing(WindowEvent e) {
      System.exit(0);
    }
  }
   addWindowListener(new WindowHandler());
   pack();
   setSize(400,400);
   setVisible(true);
  }
}

[1] Scope:The extent of the context of a definition.
[2] Local inner class: An inner class that is declared local to a block of code.

SEMrush Software