• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface

 
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


An interface can only contain method and not variables



I think the above statement is correct. Since interfaces can contain only constants and variables.


Comment.
 
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct because and interface can contain only abstract methods and fields which may not vary, in other words, constants (public static final).
 
Marcelo Ortega
Ranch Hand
Posts: 528
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i forgot to add nested interfaces and classes.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quote:

An interface can only contain method and not variables

I dont think so. An interface can have method declarations.Along with that it can have variables but of type public,final,static r default. let me know if i am wrong.

Thankz

public interface intf2 {
public void dis();
int dis1();
abstract int p();
final int i = 10 ;
static int j =20;
int k =23;
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface can contain variables, but these varaibles are static and final implicity and they must be initialised at the time of declaration itself. Interface cannot have methods with bodies they must be ended with a semicolon.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An interface can contain:
- method declarations (which are implicitly public and abstract)
- field declarations (which are implicitly public, static and final)
- type (class, interface, annotation, enum) declarations (which are implicitly public)

A final is not necessarily a constant.(When a final is not a constant). A variable is something that varies, therefore, neither a final, nor a constant are variables.
A final field is better described as 'write-once'.
A final local is better described as 'write-zero or once'.
[ July 13, 2005: Message edited by: Tony Morris ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So we can conclude that a declaration like


int i =5;


Inside a interface is known as a Constant since it is a primitive type which is initialized with a constant value and implicitly final. Hence there is no variables(Because they vary which is not possible in a interface) in a interface.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Tony]: A variable is something that varies, therefore, neither a final, nor a constant are variables.

No. That would make sense, and is a valid definition used in some other contexts. However it's not the definition of "variable" used consistently by Sun (despite what Tony says) in the JLS and elsewhere. Nothing in Sun's definitions requires that a variable actually vary.

Tony and I had this conversation several months ago. Note that Tony never provided an example of Sun's alleged contradictory use of the term variable. Also, since that last conversation, Sun has officially released the JLS 3rd edition which contains an even clearer statement of what I was saying back then, in JLS3 4.12.4: "We call a variable, of primitive type or type String, that is final and initialized with a compile-time constant expression (�15.28) a constant variable." Which according to JLS3 15.28[url] is also a constant expression.

[vishnu]: Hence there is no variables(Because they vary which is not possible in a interface) in a interface.

No. Variables are allowed in interfaces, but they must be constant variables. Which do not vary, but are (consistently) called variables by Sun anyway.
[ July 15, 2005: Message edited by: Jim Yingst ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps it is that you assume intelligence from the people at Sun, yet the only 2 that I ever knew of, left a few months ago. I assume neutrality until evidence is provided otherwise - using the expression "constant variable" in a language specification contributes to deviation from said neutrality.

I have no other explanation for what I believe is otherwise obvious.
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, still no actual evidence of your claim "and they do the opposite too."?

[Tony]: Perhaps it is that you assume intelligence from the people at Sun, yet the only 2 that I ever knew of, left a few months ago

Bloch and Gafter aside (is that who you were thinking of?), I've personally known a few others who impress me as being pretty smart. Your showing in our previous conversation on this topic was subpar at best, IMO. Note that I'd already acknowledged (before your first post) that Sun's offical definition did not quite match "common sense". Your "Since when were variables and constants not mutually exclusive?" seems to indicate you hadn't really read what the spec actually said. Or perhaps more likely, your preconceptions about what "variable" should mean were preventing you from understanding and accepting (provisionally at least) an alternate definition. If you want to say it's silly for Sun to allow the definitions of "variable" and "constant" to overlap - well, I agree somewhat. Though I do recall that back in Algebra class there was some similar confusion: in an equation like 2x + 7 = 3, is x a variable? The value is quite solidly fixed at x = -2 at that point; it doesn't vary. But I think it's somewhat natural sometimes to continue talking about x as a "variable" even in contexts where the data in question may not actually vary.

Anyway, sincd this is the SCJP forum: for those of you who just want to pass the test, I don't believe the exam will explicitly try to test you on this particular point. Though they may possibly, at some point, use the term "variable" in a way Tony doesn't approve of. Try not to panic if this happens. Just read the rest of the question, and answer it. It's not that big a deal, really.
[ July 15, 2005: Message edited by: Jim Yingst ]
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for highlighting about the new definition of constant variables in the latest JLS release. There are certain things that cannot be changed. And calling a constant a variable is one among them. Algebra example is a very good proof.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


So, still no actual evidence of your claim "and they do the opposite too."?



Sure, JLS 4.5.3. It's omission is good enough evidence to me.
In fact, if the specification were serious about a 'constant variable', it would appear at least once in Chapter 4, wouldn't you think?

My conjecture: multiple people work on the JLS, one (or few) person is not quite so bright and uses the term 'constant variable' and attempts to justify its use, somehow convincingly (or it is overlooked). Others follow, and since corporate culture mandates that the 'not so bright' people dominate and any attempt to correct is quickly quashed by the majority, the expresion remains. Call it cynicism or the 'disgruntled person' or whatever, but I lean more towards this than anything else, hence my refusal to accept a 'flimsy at best' justification.


Bloch and Gafter aside (is that who you were thinking of?), I've personally known a few others who impress me as being pretty smart.


Yes, that is who I was thinking of. I'll bet there are many more that I don't know about, but whatever the case, I will always assume neutrality.


Anyway, sincd this is the SCJP forum: for those of you who just want to pass the test, I don't believe the exam will explicitly try to test you on this particular point. Though they may possibly, at some point, use the term "variable" in a way Tony doesn't approve of. Try not to panic if this happens. Just read the rest of the question, and answer it. It's not that big a deal, really.


Agreed.
</over-it>
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Jim]: So, still no actual evidence of your claim "and they do the opposite too."?

[Tony]: Sure, JLS 4.5.3. It's omission is good enough evidence to me. In fact, if the specification were serious about a 'constant variable', it would appear at least once in Chapter 4, wouldn't you think?


Hmmmm... I gather we're talking about 2nd edition, since that was the one in effect at the time of our previous conversation (I think). The list in 4.5.3 gives seven different, mutually exclusive categories of variable. The concept of being "constant" is orthogonal to this list; constant variables can fit into categories 1, 2, and 7. I'd say it's a good thing constants weren't included in this particular list; would've been confusing. Note that primitive variables, reference variables, public, protected, and private variables aren't in this list either - probably for similar reasons.That doesn't mean there's no such thing as a primitive variable, etc. (Or variable of primitive type if you prefer.)

It might have been nice if they'd mentioned constant variables in chapter 4 in the first place, for clarity (as they eventually did in JLS3 4.12.4), but that doesn't mean there was a contradiction in the first and second editions. The rules in place established that something like Math.PI is a variable, and they also astablished that its a constant. That, in and of itself, is not a contradiction unless we import an outside definition of what is meant by "variable".
 
Oh. Hi guys! Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic