This discussion is archived
4 Replies Latest reply: Oct 16, 2006 2:06 PM by 796440 RSS

Inline Interface implementation

807607 Newbie
Currently Being Moderated
Hi everyone,

i have a question about following this reference and if it is legal or not.

Thanks,

public interface ListLoader
{
public List loadList();
}
public class Test
{

        public doSomething()
       {
                .
                .
                ListLoader loader = new ListLoader()
             {
                          public List loadList()
                    {
                         if (this instanceof XmlObject) // is this legal
                         {
                                     }
                    }
             };
                .
        }
}
  • 1. Re: Inline Interface implementation
    796440 Guru
    Currently Being Moderated
    Does it compile? If so, it's legal. If not, it's not.
  • 2. Re: Inline Interface implementation
    807607 Newbie
    Currently Being Moderated
    yes it does but logically this should refer to the inner class and not the outer class. The outer class is an instance of XmlObject not the inner class. Sorry the code does not say so, but that was the intend.

    Message was edited by:
    diesellife
  • 3. Re: Inline Interface implementation
    807607 Newbie
    Currently Being Moderated
    this is the inner class, Test.this will be the outer class.
  • 4. Re: Inline Interface implementation
    796440 Guru
    Currently Being Moderated
    yes it does but logically this should refer to the
    inner class and not the outer class. The outer class
    is an instance of XmlObject not the inner class.
    Sorry the code does not say so, but that was the
    intend.

    Message was edited by:
    diesellife
    1) What "should" happen is exactly what's described by the JLS, nohing more, nothing less. Not what you or I think is logical.

    2) It works as expected. instanceof evaluates to false.
    // ListLoader.java
    import java.util.*;
    public interface ListLoader {
        public List loadList();
    }
    
    
    
    // XmlObject.java
    import java.util.*;
    public interface XmlObject {}
    
    
    // Test.java
    import java.util.*;
    public class Test implements XmlObject { 
        public void doSomething() {
            ListLoader loader = new ListLoader() {
                public List loadList() {
                    if (this instanceof XmlObject) {
                       System.out.println("YO");
                    }
                    else {
                        System.out.println("nyet");
                    }
                    return null;
                }
            };
    
            loader.loadList();
       }
    
        public static void main(String[] args) { 
            new Test().doSomething();
       }
    }