Notes:
Local classes are classes that are defined in a block.
We typically find local classes defined in the body of a method.
Declaring Local Classes:
1 public class LocalClassExample { 2 3 static String regularExpression = "[^0-9]"; 4 5 public static void validatePhoneNumber( 6 String phoneNumber1, String phoneNumber2){ 7 8 int numberLength = 10; 9 // Local Class 10 class PhoneNumber{ 11 12 String formattedPhoneNumber = null; 13 14 PhoneNumber(String phoneNumber){ 15 // numberLength = 7; compiler error, the value of this variable cannot be changed 16 String currentNumber = phoneNumber.replaceAll( 17 regularExpression, ""); 18 if (currentNumber.length() == numberLength) 19 formattedPhoneNumber = currentNumber; 20 } 21 22 public String getNumber(){ 23 return formattedPhoneNumber; 24 } 25 } 26 27 PhoneNumber myNumber1 = new PhoneNumber(phoneNumber1); 28 PhoneNumber myNumber2 = new PhoneNumber(phoneNumber2); 29 30 if (myNumber1.getNumber() == null) 31 System.out.println("First number is invalid"); 32 else 33 System.out.println("First number is " + myNumber1.getNumber()); 34 35 if (myNumber2.getNumber() == null) 36 System.out.println("Second number is invalid"); 37 else 38 System.out.println("Second number is " + myNumber2.getNumber()); 39 } 40 41 public static void main(String[] args) { 42 validatePhoneNumber("123-456-7890", "456-7890"); 43 } 44 45 }
Accessing Members of an Enclosing Class
A local class can access its enclosing class's members, in this example, such as the member LoacalClassExample.regularExpression.
A local class also can access its enclosing method's local variable, which must not be changed by local class, such as the numberLength.
A local class also can access its enclosing method's paremeters, such as the phoneNumber1 and phoneNumber2.
Shadowing and Local Classes
The declarations of a variable in local class shadow declarations in the enclosing scope that have the same name.
1 public class ShadowTest { 2 3 public int x = 0; 4 5 class FirstLevel { 6 7 public int x = 1; 8 9 void methodInFirstLevel(int x) { 10 System.out.println("x = " + x); // 23 11 System.out.println("this.x = " + this.x); // 1 12 System.out.println("ShadowTest.this.x = " + ShadowTest.this.x); // 0 13 } 14 } 15 16 public static void main(String... args) { 17 ShadowTest st = new ShadowTest(); 18 ShadowTest.FirstLevel fl = st.new FirstLevel(); 19 fl.methodInFirstLevel(23); 20 } 21 }
Serialization
Serialization of inner classes, including local and anonymous classes, is strongly discouraged.
Local Classes Are Similar To Inner Classes
They cannot define or declare any static members.
You cannot declare an interface inside a block(such as method); interfaces are inherently static.
1 public void greetInEnglish() { 2 interface HelloThere { // not compile 3 public void greet(); 4 } 5 class EnglishHelloThere implements HelloThere { 6 public void greet() { 7 System.out.println("Hello " + name); 8 } 9 } 10 HelloThere myGreeting = new EnglishHelloThere(); 11 myGreeting.greet(); 12 }
You cannot declare static initializers or member interfaces in a local class.
1 public void sayGoodbyeInEnglish() { 2 class EnglishGoodbye { 3 public static void sayGoodbye() { // not compile 4 System.out.println("Bye bye"); 5 } 6 } 7 EnglishGoodbye.sayGoodbye(); 8 }
A local class can have static members provided that they are constant variables.
1 public void sayGoodbyeInEnglish() { 2 class EnglishGoodbye { 3 public static final String farewell = "Bye bye"; // ok 4 public void sayGoodbye() { 5 System.out.println(farewell); 6 } 7 } 8 EnglishGoodbye myEnglishGoodbye = new EnglishGoodbye(); 9 myEnglishGoodbye.sayGoodbye(); 10 }
reference from:
https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html#shadowing