javalocal-class

contradicting statement regarding to local class, which one is correct?


I'm new to Java and is trying to learn the concept of local class. I'm currently reading the chapter on local class on the Offical Java Documentation Oracle. I have encountered two statements in this chapter that seem to contradict each other. Could someone knowledgeable give me a hand as to which statement is correct? Or they are both correct, its just my understanding of the concept is not there yet.

  1. A local class has access to local variables. However, a local class can only access local variables that are declared final.

  2. Local classes are non-static because they have access to instance members of the enclosing block.

https://docs.oracle.com/javase/tutorial/java/javaOO/localclasses.html


Solution

  • Those statements are not contradicting and both are correct.

    Instance members (mentioned in the 2nd statement) are not local variables (local variables are variables which are local to some method or code block, while instance variables belong to an instance of a class, and can be accessed from any non static method of that class), so they can be accessed by the local classes regardless of whether they are final or not.