• 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

doubts on interface variables

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why interface variables are always implicitly static and final whether it is declared or not?

Thanks,
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From JLS 3.0 section 9.3:

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.



To understand why, think about the meaning of each one of the modifiers.
 
Debashree Halder
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
final means it cannot be modified and static is it can be initialized only once.

If a class implements an interface, it can implement the methods in the interface then why doesn't it change the value of the interface variable?

Thanks,
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you might want to think in interface perspective in addition to the purpose of modifiers.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think whole idea of interface is to create a place to keep common methods & variables.So that class implementing can share them.It's like a copy book or like an include.

If it is a varible only way to share it between implementing classes safely is to make it final static i.e to make it a constant value.

Regards
Tapas.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still think you guys are missing something, ie you have not really figured out why interface variables are static. Hint: consider if it makes sense to have instance variables in an interface.
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider a class Foo that extends Bar and implements Baz. As it turns out Baz also implements Baz. You can only have one copy of each object in Baz. If you could write to those variables they would be useless: you could set the value but a super-class could wipe it out anytime on any method.
 
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean "Baz also implements Baz"? Are you talking about an interface that implements itself?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Consider a class Foo that extends Bar and implements Baz.

Declare a variable "aVar" in class Bar.
And consider If u have same variable defined in the interface Baz.


What value will the class Foo inherit for the variable aVar and from which object( class "Bar" or interface "Baz" ).

Now imagine for urself the ambiguity involved if the interface varible is nonstatic and final.

Hope this clears ur doubt.

Regards
Srihari
 
Rick O'Shay
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick Portugal:
What do you mean "Baz also implements Baz"? Are you talking about an interface that implements itself?



Meant to say Foo also implements Baz, thereby introducing ambiguous fields if not static. There is no multiple inheritence in Java.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is no multiple inheritence in Java.

Well, no multiple inheritance of implementation. MI of interface is designed in...
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The shortest possible answer as to why they are static and final is "Because.
" They are, because that's how Java was designed.

Now, what's the rationale? The best rationale is simply that the creators of Java did not think of instance variables as part of the contract of any class. The only kind of public instance variables that you should ever use are the "dumb data" kind, as in java.awt.Point. Otherwise, the only sort of variables belonging to another class you should ever be interested in are public final static ones.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic