aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes interface 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 "interface" Watch "interface" New topic
Author

interface

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

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.


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

That is correct because and interface can contain only abstract methods and fields which may not vary, in other words, constants (public static final).


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD, SCEA/OCMJEA

Live life to an interface, not an implementation!
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 519

i forgot to add nested interfaces and classes.
usha prithvi
Ranch Hand

Joined: Jun 22, 2005
Posts: 31
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;
}
Pramod Arya
Greenhorn

Joined: Jul 13, 2005
Posts: 1
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.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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 ]

Tony Morris
Java Q&A (FAQ, Trivia)
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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 ]

"I'm not back." - Bill Harding, Twister
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Sep 24, 2003
Posts: 1608

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
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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".
 
 
subject: interface