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

doubts on interface variables

 
Debashree Halder
Ranch Hand
Posts: 81
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why interface variables are always implicitly static and final whether it is declared or not?

Thanks,
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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,
 
Arulkumar Gopalan
Ranch Hand
Posts: 104
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you might want to think in interface perspective in addition to the purpose of modifiers.
 
Tapas Kumar
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rick O'Shay
Ranch Hand
Posts: 531
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Rick Portugal
Ranch Hand
Posts: 243
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean "Baz also implements Baz"? Are you talking about an interface that implements itself?
 
Srihari AVAR
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no multiple inheritence in Java.
Well, no multiple inheritance of implementation. MI of interface is designed in...
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic