This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Interface variable which is not actually a variable but a constant but that is true that in if we declare a variable Suppose ======>int i=10; (is equivalent to) public static final int i=10;
I Understood the use of public ,final and also know static but
what is use of static in
I know that all stuff of static that value persist between method calls,static method can be called without any objects etc etc but what i did'nt understand is what is the use of static in interface variable as the variable in above case 'i' is 'final' then its value cannot change then why use static implicitly only final would have been enough i have figured out one use can anyone tell me the other use because i had seen the other use but i have forgotten it
Usage One I know this Use Need other Usage of static other than the given below Usage
---------- Interpretation(Java) ----------
Value i Demo=10
Value i Demo2=20
The question above was not how to use a non static variable in ambiguous situations but the question was "what is the use of implicit static declaration of variable in an interface when only final would be enough," please read carefully the deep meaning of the question to answer i did understand the question joy but i do not know the answer may people from sun microsystems could answer that because they are the one who made interface variable static.
for information interface variable is
public and static and Final
PowerPuff Girls wrote:The question above was not how to use a non static variable in ambiguous situations but the question was what is the use of implicit static declaration of variable in an interface when only final would be enough ,please read carefully the deep meaning of the question to answer
Well I don't think there is any deep meaning in this question. The question is clear to surface. I told that example to show that the reason that he thinks why interface fields are static is not right. And the reason why I think that the fields of an interface are static is because an interface cannot be instantiated. So whats the use of fields in it. Suppose the fields are not static, then they are public non-static fields. That is like breaking encapsulation. And what is the use of non-static final fields when the scope in which you declare it cannot have a constructor (I mean interface). In that case you'll have to assign it a value at the time of declaration. So that is like a non-static field whose value is decided right where it is declared. So the value will remain same for all instances. So it is better to keep the field static as its value will be the same for all instances...
Unfortunately, I dont have exact and a precise answer to this question. But, I can recall a few statements that I have read about static fields and interfaces that I think are worth mentioning here(correct me lest I am wrong). These are as follows:
1. Interfaces are abstractions and a medium used to define (generic)contracts. It lists the behavior that the implementing classes will have. The contract is not concerned with implementation details. So, the interface definition(what you write in interface body) should not contain anything that serves as a binding for implementing classes.
2. Anything non-static is considered an implementation detail(personal opinion). Static fields are not bound to any implementation. They work at a higher level than instance variables. For e.g: instance variables, an implementation of chair(abstraction) will have values for color(instance variable). Besides, as Ankit mentioned Interfaces cannot be instantiated.
3. Fields of an Interface as supposed to be symbolic constants that can be used in implementations of the interface methods. Therefore, these fields should be constants(final), since they can be referred fom various places.