?
Solved

I just want to understand this Java Interface souce code

Posted on 2012-08-27
1
Medium Priority
?
279 Views
Last Modified: 2012-10-11
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.
0
Comment
Question by:Robert Silver
1 Comment
 
LVL 36

Accepted Solution

by:
mccarl earned 2000 total points
ID: 38339134
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month16 days, 17 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question