In the java 8 tutorial on local class it says the following about local class in static methods:
Local classes are similar to inner classes because they cannot define or declare any static members. Local classes in static methods, such as the class PhoneNumber, which is defined in the static method validatePhoneNumber, can only refer to static members of the enclosing class. For example, if you do not define the member variable regularExpression as static, then the Java compiler generates an error similar to "non-static variable regularExpression cannot be referenced from a static context."
Local classes are non-static because they have access to instance members of the enclosing block. Consequently, they cannot contain most kinds of static declarations.
I am confused by the error message in paragraph 1 and what is stated in paragraph 2, why the local class is considered static context but itself is non-static? Local variables and parameters in static methods are not intended for use with class instance, so why can't we make a local class in static method static?
The example it uses is shown below
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");
}
}
For the following code sample, I am able to access printMember() of AnotherClass from the static method localMethod() of the class LocalClassTest, so I am assuming that even the local class is declared static, I can still access enclosing class' static members with class reference
public class LocalClassTest {
private static final int INTEGER = 9;
private static String name = "foo";
public static void localMethod() {
int localInt = 5;
//Accessing class variables of its own class
System.out.println("name = " + LocalClassTest.name + " and INTEGER = " + LocalClassTest.INTEGER);
//Accessing class variables of another class
//If a static method can access static members of another class,
//why cann't we declare a local class static and access class variable through class reference?
AnotherClass.printMembers();
class LocalClass {
public void printAccess() {
System.out.println("Variable of local static method, localInt = " + localInt);
// if the localClass were static, would I be able to access class variable name
// through class reference LocalClassTest.name?
System.out.println("Class variable of enclosing class, name = " + name);
}
}
LocalClass obj1 = new LocalClass();
obj1.printAccess();
}
public static void main(String[] args) {
LocalClassTest.localMethod();
}
}
class AnotherClass {
public static String name = "class variable from another class";
public static void printMembers() {
System.out.println("name = " + AnotherClass.name);
}
}
Thanks for your help.