In Java, is there a way to explicitly prevent an anonymous class to reference the outer class or method's members/local variables?
            Asked
            
        
        
            Active
            
        
            Viewed 172 times
        
    2
            
            
        - 
                    4Don't make it an anonymous class? – Elliott Frisch Nov 13 '15 at 02:50
 - 
                    3Why is this a thing you want? – Eric Nov 13 '15 at 02:51
 - 
                    1Don't believe so, because you can always do this from the inner class: `Class.this.member` – Wyatt Lowery Nov 13 '15 at 02:52
 - 
                    An anonymous class _always_ has a reference to the enclosing outer class. To avoid that you'll have to create it in a static method or create a named static inner class. – Louis Wasserman Nov 13 '15 at 02:54
 - 
                    1http://stackoverflow.com/questions/758570/is-it-possible-to-make-anonymous-inner-classes-in-java-static – LINEMAN78 Nov 13 '15 at 02:55
 - 
                    It sounds just like asking : how do I prevent instance method to access instance fields... – Adrian Shum Nov 13 '15 at 02:55
 - 
                    You could create an entirely class that `extends`/`implements` the anonymous class's type. If the fields in the other class were `private`, they wouldn't be referenceable from outside that class. Hope that made sense. – Lucas Baizer Nov 13 '15 at 02:56
 - 
                    1sigh, why is this being downvoted/closed? it's a legitimate, objective, specific, programming question that has potential for others as well as the OP. – jb. Nov 13 '15 at 03:02
 
1 Answers
1
            
            
        There isn't. You always define anonymous classes inside other classes, e.g.
class A {
    private String aMember;
    public void test() {
        B b = new B() {
            @Override
            public void b() {
                ...
            }
        };
    }
}
You can always use OuterClassName.this.something to access the outer class:
@Override
public void b() {
    A.this.aMember = "Hello";
}
Why do you want to restrict access to the outer class? Once we know that, we could better understand what you're trying to achieve.
        MC Emperor
        
- 22,334
 - 15
 - 80
 - 130
 
- 
                    So with Java 8, I want to pass lambda like Function to some places, and I want the lambda to be exactly a function of input to output, not able to use something else outside the lambda. – Pinch Nov 13 '15 at 03:03
 - 
                    @Pinch: Why do you want that? Are you aiming to enforce "pure" functions? You're onto a loser, because I can still `new Random().nextInt()` or `MySingletonClass.getSomethingElse()` – Eric Nov 13 '15 at 03:07
 - 
                    @Pinch A lambda is not an anonymous class. There is no reference to an enclosing instance. They only hold onto references explicitly used in the body. – Paul Boddington Nov 13 '15 at 03:20
 - 
                    @PaulBoddington: is that behavior of lambda not referencing the outer class instance documented? – Pinch Nov 13 '15 at 18:51
 - 
                    @Pinch Read this: http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html The relevant bit is 7: Variable Capture, but the whole thing is worth reading. – Paul Boddington Nov 13 '15 at 19:07