aspose file tools*
The moose likes Beginning Java and the fly likes what is use implicit static in interface constant Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "what is use implicit static in interface constant" Watch "what is use implicit static in interface constant" New topic
Author

what is use implicit static in interface constant

Joywish Manampadi
Greenhorn

Joined: Jan 23, 2009
Posts: 6
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

Output completed (0 sec consumed)

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

Well Joywish the ambiguous call is some what wrong. If i was not static, then you would have not been able to access it in the static main method anyways. Lets modify your code



Now you can access i in the myMain method directly. And the compile time error can be solved like this



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ashutosh Chauhan
Greenhorn

Joined: Feb 26, 2009
Posts: 14
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

[edit]Get rid of different size print. CR[/edit]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36497
    
  16
"PowerPuff Girls" please read the important administrative private message I just sent you.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

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...
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369

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.

4.
The materials of action are variable,but the use we make of them should be constant - JLS Chapter 9 - Section 9.3



Note: The link in quotes is not to the book but to a web page that proves that the preceding text is indeed part of the book ;)

[List of FAQs] | [Android FAQ] | [Samuh Varta]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Agree with Ankit
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18117
    
  39

"Benjamin Button",

Please reread the administrative message that Campbell sent. Obviously fake names are not allowed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is use implicit static in interface constant
 
Similar Threads
What's an interface?
interfaces
Interface
superclasses- initializing it's fields in the subclass
David Smalley