tags:

views:

56

answers:

2
public class A {
    static class B {
    }
}

public class C {
   void m(X x) {
      if (x instanceof A.B) { ... } // not working
   }
}

Is there a way to verify if some object (x) is actually the inner class (B) of some class (A) in this scenario?

I'm getting this error, but I unfortunately have no control over the classes A and B.

The type A.B is not visible

+2  A: 

I made this answer as a comment, but with a little reflection (the thinking kind, not the coding kind!), I'm turning it into an answer.

Since you don't have control over A or B, and B has package access, you can only see it from classes that are in the same package as A. So what you could do - if you don't want to move C into the same package - is write a utility class - call it U - that has a boolean function, taking an X and returning whether it's an instance of A.B.

Carl Manaster
+1: A little reflection (the coding kind, not the thinking kind) might actually be able to do this. But it's not the way to go.
Don Roby
What do you mean by that? I added a new package with the same name as the package in which A is located to my project and am able to access A.B within a helper class.
HTTPeter
I'm afraid I don't understand the question - or even whether it's for me or @donroby.
Carl Manaster
Sorry I was asking what @donroby meant to say because I didn't understand his question.
HTTPeter
He meant tha Java reflection could be used for this, but it would be a bad way to go.
Carl Manaster
Sorry if I introduced confusion here. I was mainly riffing on Carl's phrase about reflection. Carl's explanation of my meaning is correct.
Don Roby
A: 

You can do like this.Both class A,C and Z resides in same package.

public class A { 

    static class B 
    { 
    } 
} 

    public class C {

        static void  m(Z x) { 

            System.out.println( x.getClass() + "^^^" +new A.B().getClass() );
            **if (x.getClass().equals(new A.B().getClass()))** 
              { 

              }
              // not working 
        } 


        public static void main(String[] args)
        {
            Z x = new Z(); 
             m(x);
        }
    }

    class Z
    {


    }
Suresh S