aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Inferface implicit access 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 "Inferface implicit access" Watch "Inferface implicit access" New topic
Author

Inferface implicit access

Dave Wheeler
Greenhorn

Joined: May 21, 2008
Posts: 4
I'm studying the Sun Certified Programmer for Java 5 book by Sierra and Bates and I found what seems like a contradiction about the implicit access granted to interface methods. The first bullet on the bottom of page 20 states "All interface methods are implicitly public and abstract." Which to me means that if you don't explicitly assign an interface method public access, it will automatically get assigned public access. However, on page 21 (a little more than half way down) it states "The public modifier is required if you want the interface to have public rather than default access." So, what access does a method have if it is not explicitly defined as public?

Thanks in advance.
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Hi Dave,

unfortunately I don't have the book at hand but the Java language specification clearly says:
All interface members are implicitly public. They are accessible outside the package where the interface is declared if the interface is also declared public or protected, in accordance with the rules of �6.6.


Access to interface members can only be public or default access (package private) but it's public by default. So I think your second statement was mentioned within another context in this book.

Marco
Dave Wheeler
Greenhorn

Joined: May 21, 2008
Posts: 4
Thanks for the quick response Marco.

After looking at it more closely, I noticed that the statement on page 21 is referring to the interface declaration itself, not an interface method declaration. So, that (and your response) clears it up for me.

Dave
Shannon Lal
Greenhorn

Joined: May 13, 2008
Posts: 5
I am preparing for the JP 5 exam and in some of the material I am reading it indicated that constants defined in a Java Interface must be defined as
public static final. I was just writing some code and I was able to create a class with a constant that was only public static.

I am missing something?

public interface TestInterface {
public static String member = null;
}
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

Originally posted by Shannon Lal:
I am preparing for the JP 5 exam and in some of the material I am reading it indicated that constants defined in a Java Interface must be defined as
public static final. I was just writing some code and I was able to create a class with a constant that was only public static.

I am missing something?

public interface TestInterface {
public static String member = null;
}


Interface variables are public static final by default. You need not explicitly mention it


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Shannon Lal
Greenhorn

Joined: May 13, 2008
Posts: 5
Yes but if I define a field in an Interface as only "public static" shouldn't the compile complain about that. Don't fields in interfaces need to be public static final?

Thanks
Dave Wheeler
Greenhorn

Joined: May 21, 2008
Posts: 4
Shannon, the public static final modifiers or optional for interfaces, so the compiler doesn't care if you explicitly code them or not. You can explicitly code any or all of them if you want, but you don't have to (as it's redundant). When you don't explicitly code any or all of them, they are assigned implicitly (automatically).

Dave
Dave Wheeler
Greenhorn

Joined: May 21, 2008
Posts: 4
Oops, I just noticed a typo: "or optional for interfaces" s/b "are optional for interfaces".
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

Shannon,

Perhaps the Java Language Specification will make it clearer:


Every field declaration in the body of an interface is implicitly public, static, and final. It is permitted to redundantly specify any or all of these modifiers for such fields.


It's not a compiler error to redundantly specify any or all of the implicitly modifiers for a field in an interface, because the JLS explicitly says it's permitted.

As a matter of personal preference, I recommend redundantly specifying the implicit modifiers, to make it easy and clear to anybody reading the code, instead of expecting everyone to remember all these rules. But it's very important to understand them.


There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inferface implicit access