3

I've seen this interface declaration in https://developer.android.com/training/basics/network-ops/connecting.html:

public interface DownloadCallback<T> {
    interface Progress {
        int ERROR = -1;
        int CONNECT_SUCCESS = 0;
        int GET_INPUT_STREAM_SUCCESS = 1;
        int PROCESS_INPUT_STREAM_IN_PROGRESS = 2;
        int PROCESS_INPUT_STREAM_SUCCESS = 3;
    }
    void updateFromDownload(T result);
    ...
}

As per https://docs.oracle.com/javase/tutorial/java/IandI/interfaceDef.html, interface body can only contain

  • abstract methods
  • default methods
  • static methods
  • constant declarations
  • In this case, the interface body contains another interface. How do you interpret this code block? Can someone please point me to the right documentation so I can learn more about this approach?

    8
    • 1
      javatpoint.com/nested-interface Commented Apr 7, 2018 at 9:58
    • weird that the official oracle docs did not include this. Commented Apr 7, 2018 at 10:00
    • 1
      @user1506104 That's a tutorial which does not mention all the possibilities. Commented Apr 7, 2018 at 10:00
    • where do you guys check the complete java language docs? thanks anyway. sorry to bother you with this useless question. better remove this now. Commented Apr 7, 2018 at 10:03
    • 1
      thank you guys. i've learned a very important lesson today. cheers! ^.^ Commented Apr 7, 2018 at 10:18

    1 Answer 1

    2

    See JLS §9.1.4:

    The body of an interface may declare members of the interface, that is, fields (§9.3), methods (§9.4), classes (§9.5), and interfaces (§9.5).

    Usage is simply OuterInterface.NestedInterface.

    In the code you've quoted the nested interface is only used to contain the constants so that they don't "leak" to classes implementing DownloadCallback.

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

    Comments

    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.