1

I have a local class...

public class Outer {
    public void wrapper() {
        class Local {
        }
    }
}

and I have a test that needs to reference the local class...

Outer.wrapper.Local.class ## this doesn't seem to work

How can I reference the local class?

13
  • 2
    Just don't declare classes inside functions. Commented Aug 23, 2018 at 14:47
  • 1
    You can't. The class only exists within the scope of the function. Commented Aug 23, 2018 at 14:49
  • 3
    local classes are what their name says: local. They can't be referred outside Commented Aug 23, 2018 at 14:49
  • 2
    @Lino "can't be used outside" is not quite true: you can, say, return an instance of the local class, and then do... Whatever. But you can't refer to that class by name. Commented Aug 23, 2018 at 14:51
  • 1
    @AndyTurner that's what I actually meant, sorry. Changed the comment to say refer Commented Aug 23, 2018 at 14:52

2 Answers 2

3

You can only reference a Local Inner class inside the method in which you have declared it:

public void wrapper() {
  class Local {

  }
  Local obj = new Local();
}

This classes tend to not be very useful due to their limited scope. If you have found a valid use case to define one take a look at this tutorial.

Sign up to request clarification or add additional context in comments.

5 Comments

So local classes are implicitly private.
@nobar JLS 14.3. Local Class Declarations prohibits modifiers so writing private class Local would trigger a compilation error. I'd say they are their own special bread.
@nobar no, they are local: they are as accessible as local variables (private classes are accessible, for example, in the same compilation unit). If you want to give it a label, that might be "really really private".
According to JLS 14.3 "Every local class declaration statement is immediately contained by a block". Using this wording I would say they are block private. But "really really private" sounds better :)
block private is what I had in mind -- or private to the enclosing scope, which is the block.
0

Local Class (a.k.a. Local Inner Class or Method-Local Inner Class):

Local class is defined as an inner class within a method. Like local variables, a local inner class declaration does not exist until the method is invoked, and it goes out of scope when the method returns. This means its instance can be created only from within the method it is declared in.

This inner class can be instantiated only after its definition (i.e., the instantiation code must follow the declaration). The inner class do not have access to local variables of the method unless those variables are final or effectively final.

Here is an example:

int length = 10; // outer class's instance variable

public void calculate() {
    final int width = 20;
    class Inner { // inner class definition
        public void area() {
            System.out.println(length * width);
        }
    }
    Inner local = new Inner(); // this statement comes only after local class's definition
    local.area();
}

NOTES:

  • The only modifiers that can be applied to a method-local inner class are abstract and final, but never both at the same time.
  • A local class declared within a static method has access to only static members of the enclosing class, and no access to instance variables.

5 Comments

This reads like copy&pasted from some tutorial, and it does not even answer the question of how to access that local class from outside the method.
The answer post content is not from a tutorial. It is from my own notes which I had prepared for my Java SE 8 certification exam, sometime last year. I used it to get thru my exam. The answer shows how to use the method local class properly and some points to consider. The OP has the question as How can I reference the local class? And, the post's title says How to reference local class.
I was specifically interested in referencing the local class from a separate test class. Sorry if I wasn't clear about that.
@nobar One can reference an inner class from another class; but not a method local class. There are other (not local) type of inner classes to consider - regular inner class or a static nested class.
@nobar Here is the link to the Oracle's Java tutorials topic Nested Classes. This has detailed info about Nested classes in general and specifically of each type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.