I have the following code from the java oracle documentation:
public class LocalClassExample { static String regularExpression = "[^0-9]"; public static void validatePhoneNumber( String phoneNumber1, String phoneNumber2) { final int numberLength = 10; // Valid in JDK 8 and later: // int numberLength = 10; class PhoneNumber { String formattedPhoneNumber = null; PhoneNumber(String phoneNumber){ // numberLength = 7; String currentNumber = phoneNumber.replaceAll( regularExpression, ""); if (currentNumber.length() == numberLength) formattedPhoneNumber = currentNumber; else formattedPhoneNumber = null; } public String getNumber() { return formattedPhoneNumber; } // Valid in JDK 8 and later: // public void printOriginalNumbers() { // System.out.println("Original numbers are " + phoneNumber1 + // " and " + phoneNumber2); // } } PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1); PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2); // Valid in JDK 8 and later: // myNumber1.printOriginalNumbers(); if (myNumber1.getNumber() == null) System.out.println("First number is invalid"); else System.out.println("First number is " + myNumber1.getNumber()); if (myNumber2.getNumber() == null) System.out.println("Second number is invalid"); else System.out.println("Second number is " + myNumber2.getNumber()); } public static void main(String... args) { validatePhoneNumber("123-456-7890", "456-7890"); } }
In this part of the official java documentation
Local classes are classes that are defined in a block, which is a group of zero or more statements between balanced braces. You typically find local classes defined in the body of a method.
I understand that a local class are classes defined in a block, etc., but I don't understand their purpose in general.
- What could you use them for?
- Are they similar to java anonymous classes or are they different?
What is a local class?
According to the doc :
A local class is declared locally within a Java code block, rather than as a member of a class. Typically, a local class is defined inside a method, but it can also be defined inside a static initializer or an instance initializer of a class.
Because all Java code blocks appear inside class definitions, all local classes are nested inside the classes they contain. For this reason, local classes share many of the characteristics of member classes. However, it is often more appropriate to think of them as a class entirely separate from the inner class. A local class has roughly the same relationship to a member class that a local variable has to a class instance variable.
The defining characteristic of a local class is that it is local to a block of code. Like a local variable, a local class is only valid within the scope defined by its enclosing block.
If, for example, a member class is used only within a single method of its containing class, there is usually no reason why it cannot be coded as a local class, rather than a member class.
The example below shows how we can modify the LinkedStack class's enumerate() method so that it defines Enumerator as a local class instead of a member class. By doing this, we move the class definition even closer to where it is used and hopefully improve the clarity of the code much more. For brevity, the example shows only the method
enumerate()
, not the entire classLinkedStack
that contains it.Characteristic
Characteristics of local classes
Local classes have the following interesting features:
Like member classes, local classes are associated with a containing instance and can access all members, including private members, of the containing class.
In addition to accessing fields defined by the containing class, local classes can access any local variables, method parameters, or exception parameters that are within the scope of the local method definition and declared final (
final
).restrictions
Local classes are subject to the following restrictions:
A local class is visible only within the block that defines it; can never be used outside of that block.
Local classes cannot be declared public, protected, private, or static. These modifiers are for class members; they are not allowed with local variable declarations or local class declarations.
Like member classes, and for the same reasons, local classes cannot contain static fields, methods, or classes. The only exception is for constants that are declared static and final.
Interfaces cannot be defined locally.
A local class, such as a member class, cannot have the same name as any of its enclosing classes.
As mentioned above, a local class can use the local variables, method parameters, and even exception parameters that are in its scope, but only if those variables or parameters are declared final. This is because the lifetime of an instance of a local class can be much longer than the execution of the method in which the class is defined. For this reason, a local class must have a private internal copy of all the local variables it uses (these copies are automatically generated by the compiler). The only way to ensure that the local variable and the private copy are always the same is to insist that the local variable is final.
When to use them?
The Java doc says the following
Helpful Links
Class Taxonomy
This image shows the taxonomy of classes in Java:
They are similar in that, outside of the code where they are defined, you cannot reference them.
The difference is that with an anonymous class you can't reference it even where you define it; you can only create one instance.
The code shows you a good example; you create multiple instances of
PhoneNumber
and define variables with that type. With anonymous classes you would find that:You can only create one instance for each class
You can't define a variable of that class, you can only use a superclass (this means that, although possible, defining new public methods in an anonymous class doesn't make sense; unless you use reflection you won't be able to call them).
Having said that, I personally am in favor of not abusing anonymous and local classes for readability reasons, and in the case of local ones I always prefer to use a nested class , defined at the class level. But it is a matter of preference.
The purpose of a nested class is, for you to understand, a method with enough complexity to decide to convert it into a class, in this way the behavior is different, having said this, I answer:
the nested class only exists as long as the outer class exists, you can't make an egg without breaking the shell, that explains it all