File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inferface implicit access

 
Dave Wheeler
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1294
IntelliJ IDE Java Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6663
5
Firefox Browser Linux MyEclipse IDE
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shannon Lal
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops, I just noticed a typo: "or optional for interfaces" s/b "are optional for interfaces".
 
Stevi Deter
Ranch Hand
Posts: 265
Hibernate Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic