• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

what is use implicit static in interface constant

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]
 
Marshal
Posts: 75836
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"PowerPuff Girls" please read the important administrative private message I just sent you.
 
Ankit Garg
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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...
 
Rancher
Posts: 1369
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ;)
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Agree with Ankit
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Benjamin Button",

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

Henry
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic