File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubts on interface variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubts on interface variables" Watch "doubts on interface variables" New topic
Author

doubts on interface variables

Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
Why interface variables are always implicitly static and final whether it is declared or not?

Thanks,


Debashree Halder<br />SCJP 1.5<br />Washington DC
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Debashree Halder
Ranch Hand

Joined: Jul 27, 2005
Posts: 81
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

Joined: Oct 13, 2003
Posts: 104
Perhaps you might want to think in interface perspective in addition to the purpose of modifiers.


Anbudan & Mahalo,<br />Arul<br /> <br />-Not a sun certified Java professional :-)
Tapas Kumar
Greenhorn

Joined: Jul 29, 2005
Posts: 6
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

Joined: Sep 29, 2002
Posts: 1683
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.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
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

Joined: Dec 17, 2002
Posts: 243
What do you mean "Baz also implements Baz"? Are you talking about an interface that implements itself?


IBM 286, SCJP, SCWCD, EIEIO
Srihari AVAR
Greenhorn

Joined: Apr 29, 2002
Posts: 4
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

Joined: Sep 19, 2004
Posts: 531
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

Joined: May 22, 2003
Posts: 657

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
jQuery in Action, 2nd edition
 
subject: doubts on interface variables
 
Similar Threads
interfaces
strictfp
Static methods in an interface
Strange question
Interface