Local classes¶
Basics¶
Local classes are classes declared in a code block, such as a method, a for loop, or an if statement. When declaring a local class, we omit information about the access modifier. Local class objects have access to the method fields of the parent classes. The next example shows the use of a local class directly in the main
method:
public static void main(String[] args) {
final List<String> names = List.of("Kate", "Maggie", "Peggie");
final List<String> surnames = List.of("Jones", "Smith", "Budden");
final int someVariable = 3; // variable to represent the access of local class variables
class Name { // creating a local class definition, without an access modifier
private final String firstName;
private final String lastName;
public Name(final String firstName, final String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getReadableName() {
System.out.println("Hey I can use outer variable " + someVariable);
return firstName + " " + lastName;
}
}
for (int idx = 0; idx < names.size(); idx++) {
final Name name = new Name(names.get(idx), surnames.get(idx)); // use of local class
System.out.println(name.getReadableName());
}
}
NOTE: Interfaces cannot be declared in a code block.
However, local classes are not as flexible as "regular" [classes] (../javaBasics/object_classes.md). Well:
- cannot define static methods
- cannot contain static fields
- but they can contain static constants, i.e. those with the
static final
modifier
The example below shows the limits of local classes:
class InnerClassExample {
private static final String APP_NAME = "DummyApp"; // OK
private static String INCORRECT_FIELD = "IAmMissingFinal"; // Compilation error, no final modifier
public void printAppName() {
System.out.println(APP_NAME);
}
public static void shouldNotBeDeclaredHere() {} // Compile error, static method in local class
}