I just want to understand this Java Interface souce code

This is straight Java code:

public interface StockPriceService extends RemoteService
{
      StockPrice[] getPrices(String[] symbols);

      /**
       * Utility class for simplifying access to the instance of async service.
       */
      public static class Util
      {
            private static StockPriceServiceAsync instance;
            public static StockPriceServiceAsync getInstance()
            {
                  if (instance == null)
                  {
                        instance = GWT.create(StockPriceService.class);
                  }

                  return instance;
            }
      }
}
-- I used to think an interface could not contain any instantiation code but surprise it appears it can almost like abstract class code.

Note the above so My question is does this Interface when implemented insert
an inner class into a given class that implements the Interface?

What does     class   AppleFun  implements StockPriceService  do?
Unlike typical interfaces witho classes ebedded will this in essence insert an inner class
definition in the same way an abstract class inserts specifically defined methods ?

Anotherwords does the above code mean that
AppleFun class has an inner static class called, Util exactly as deined above?

I had never seen anything like this with interfaces. Can anyone comment on this?
Or confirm my expecation that this inner class contained in an interface is part of any
class that implements the  interface?  
Does anyone find this code a bit odd? Maybe it was never covered in my JDK book or maybe its part  of the 1.7 JDK standard and on part of the earlier standards.

Maybe I just never came across such a usage case before.
LVL 2
Robert SilverSr. Software EngineerAsked:
Who is Participating?
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
I used to think an interface could not contain any instantiation code but surprise it appears it can
I know what you are saying here but the Interface itself doesn't have any implementation, the inner class does and this is essentially separate to the interface other than the pact that it is packaged within the interface's definition.

does this Interface when implemented insert an inner class into a given class that implements the Interface?
No, the inner class only ever resides in the interfaces definition, so in your example of "AppleFun implements StockPriceService" then no, you can't refer to AppleFun.Util as a class, only StockPriceService.Util.

As for which versions it is allowed in, at least Java 6, I just tested it to be sure.

Think of inner classes just as separate classes but with heirarchial/shared naming and a few special properties in terms of what fields/members can be accessed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.