• 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

spot the difference: public static final

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone. I'm sure you'll all agree with me that interface field members are implicitly "public static final". But why is public static final transient allowed in a class whereas transient variables (read as public static final transient) are not:

Can somebody please spot the difference for me... Are static, final and transient really are allowable combinations for fields?
[ March 19, 2004: Message edited by: nikki lorenzo ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the JLS, §9.3 Field (Constant) Declarations:


ConstantDeclaration:
ConstantModifiersopt Type VariableDeclarators

ConstantModifiers:
ConstantModifier
ConstantModifiers ConstantModifer

ConstantModifier: one of
public static final

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.


As you can see from this, all fields declared within an Interface are implicitly public, static, and final, but they are not allowed to be transient.
In addition, I think it's more important to realize what these modifiers mean as that will help you remember when and where you can (and should) use them.
For example, what does transient mean? The keyword transient indicates that this piece of data is not part of the persistent state of an object. Because you can never have an instance of an Interface, what sense does it make for a variable to be decalred transient within an Interface.
So, in short, the difference is that one variable is declared within a class (which can be serialized and therefore the transient keyword makes sense) while the other is declared within an Interface, where the keyword makes no sense.
I hope that helps,
Corey
 
nikki lorenzo
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for pointing that out, Corey.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic