Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes member interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "member interface" Watch "member interface" New topic
Author

member interface

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Hi,

class ppp {
static class StaticNested { interface ABC {}} // 1
class NonStaticInner {interface DEF {}} // 2
interface GHI {} // 3
}
A compile-time error is generated at which line?

a. 1
b. 2
c. 3
d. None of the above

Answer is b as member interface is implicitly static.But I would say option c also will give compile time error.

I verified the java language specification that says




Please clarify my doubt.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
If you remove line 2 from your code it compiles without problems. So option c) is wrong.

If nested interfaces are implicitly static, then it means that line 3 is the same as:



which compiles without problems.

The JLS example is a little misleading. The code


compiles. The interface NeverInner is not an "inner" interface (there is no such thing), it is a "static nested" or "nested" interface.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
There is no such thing as an inner interface. Because, interfaces are always implicitly static. They are always top-level, not inner. Inner classes cannot have any static modifiers at all.

Shiva Mohan
Ranch Hand

Joined: Jan 05, 2006
Posts: 479
Thankyou very much.now i got the misleading.
 
Don't get me started about those stupid light bulbs.
 
subject: member interface