• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inferface implicit access

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, I just noticed a typo: "or optional for interfaces" s/b "are optional for interfaces".
 
Ranch Hand
Posts: 265
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic