VextoR
VextoR

Reputation: 5165

How to understand that an inner object is instanceof outer class?

I want to see if innerObj is a kind of instanceof outerClass

in the example al instanceof F will return false, but I would like to see if al object is part of F class

example:

public class TestClass
{


    public static void main(String[] args)
    {
        F f = new F();
        ActionListener al = f.getAL();
        System.out.println(al.getClass()); //prints class F$1. Close, but it's a String..
        if (al instanceof F) {
            //false, but I want to understand that "al" was created in "F"
        }
    }
}

class F {
    ActionListener al;
    
    F(){
        al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        };
    }
    
    public ActionListener getAL() {
        return al;
    }
}

Upvotes: 0

Views: 87

Answers (1)

Louis Wasserman
Louis Wasserman

Reputation: 198023

You're almost there. You're looking for al.getClass().getEnclosingClass().

(That said, it's very strange that you want to do this check, and it seems pretty fragile...)

Upvotes: 4

Related Questions