aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes final static int i =0 Whizlabs says i is variable and i feel it is a constant 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 "final static int i =0 Whizlabs says i is variable and i feel it is a constant" Watch "final static int i =0 Whizlabs says i is variable and i feel it is a constant" New topic
Author

final static int i =0 Whizlabs says i is variable and i feel it is a constant

Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
in 1 of the whizlab practise test, a question is liks this
which of the following is true?
A ) An interface can only contain methods and not variables
B ...
C ...
D ...

I marked A as true and it is false according to them.
could sun exam developers say the final words whether static final i = 1 here i is constant or variable


Aruna
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
An interface can contain method declarations and constants, but not method implementations or variables. Constants are variables defined as final. So, A is incorrect because it can contain constants, not "only methods".


Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 804
By the way, the static and final are redundant as method constants are implicity public static final.
Amit Das
Ranch Hand

Joined: Mar 05, 2005
Posts: 206
i also ot aruna for the fact about consants not same as variables...
for reference plz refer....
http://java.sun.com/docs/books/tutorial/java/interpack/createinterface.html

Definition: An interface is a named collection of method definitions (without implementations). An interface can also declare constants.


i think its debatable....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Amit - you're right that constants are not the same as variables - but that doesn't mean they're mutually exclusive terms either. (Even though common sense seems to indicate "constant" and "variable" are opposite, they aren't.) Some variables are constants, and some are not; some constants are variables, and some are not. Interfaces may contain methods and constant fields; these constant fields are variables which are also constants. Therefore the statement "An interface can only contain methods and not variables" is false.


"I'm not back." - Bill Harding, Twister
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
ok is the following statement true or false?

1) interface contains variables

I feel it is false as variables can never be constants and constants can never be variables. they are exclusive.
constant is that which cannot vary
variable is that which can vary

i am awaiting enlightenment )
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Aruna]:i am awaiting enlightenment

Well, you could reread my post. Variables and constants are not mutually exclusive, despite what common sense might say. Check the definition of a constant expression. The last two bullets allow variables, provided those variables are final and are initialized with other constant expressions. So something like Math.PI is both a variable, and a constant, according to the definitions we use in Java.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
Thanks Jim for enlightenment!
On similar grounds , we can say that
A non static inner class may have static members. is a TRUE ... am i right??
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Yes. But inner (nonstatic) classes can only have static members if (a) they are final fields initialized with compile-time constant expressions, or (b) they are inherited from a (top-level or static) superclass.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Since when were variables and constants not mutually exclusive?
You might mean final and constant is not mutually exclusive?
A final is a type of variable - the restriction being, it varies once - sometimes a final is called "write-once only". A final is not a constant. A constant is a special type of final. A constant is not a type of variable like a final. A constant is not "write-once only". A constant is defined by JLS 15.28.

This points out the difference between a final and a constant: http://forum.jtiger.org/posts/list/1.page


Tony Morris
Java Q&A (FAQ, Trivia)
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Tony]: Since when were variables and constants not mutually exclusive?

Ummm... you said it yourself. A final is a type of variable. A constant is a special type of final (which is a type of variable). Some variables are final. Some finals are constant. Therefore, some variables are constant. Variables and constants are not mutually exclusive. Math.PI is an example of a variable that is also constant. As are the various fields of the interface java.io.ObjectStreamConstants (to give an example that's more directly relevant to this thread). OK?

Of course, the preceding syllogism ignores the fact that there are also constants which are not variables. (E.g. literals like 1 or "foo".) And we could nitpick over the distinction between a symbol and the thing it refers to. But that would be rather tedious, no?

Incidentally, you might note that I've already referenced JLS 15.28 in this thread, and specifically pointed out the part about variables. It's not a new concept in this thread. If you've got a different take on it, please let us know how it's different from what I've said, with respect to the discussion so far.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
A variable varies.
A final may or may not be a constant.
A constant is always a final.
A final is a write-once variable, unless it happens to a constant, in which case, it is a constant, and not a variable.
A constant is not a write-once variable.
A constant never varies.
A constant is not a variable.
A variable is not a constant.
A constant is mutually exclusive to a variable.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Tony]: A variable varies.

That would certainly make sense, but it's not necessarily true in Java. (This is why I twice previously referred to "common sense" and how it's at odds with the way these terms are used in Java.) Please, Tony, just look at the oft-referenced JLS 15.28, specifically the last two bullets in the list, and then explain to us how the term "variable" refers to something that is defined (in the very text under discussion) as a constant.
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
This is the 43.E+256 time a problem has been found in the JLS.
The majority of these problems are misuse of terminology.
What makes this one any different?
Shall we break out the "fundamentals of computer science" books?
Nikhilesh Fonseca
Ranch Hand

Joined: Aug 23, 2004
Posts: 57
When you declare a class in an interface you can declare variables in that class and so adjust the values in the methodsof the class hence variables can also be present in a class ..
Sorry I havent gone thru all the posts they were so many..If this is already covered.. A Thousand Apologies
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
Originally posted by Jim Yingst:
Yes. But inner (nonstatic) classes can only have static members if (a) they are final fields initialized with compile-time constant expressions, or (b) they are inherited from a (top-level or static) superclass.


I know the conditions u listed as a) and b).. my dilemma is i am giving SCJP 1.4 after 2 days and if a question like is it true or false type comes with

non-static inner class can have static variables(true/false) what should i answer to get marks...
so leaving behind conditions a) and b), pls just say true or false .

eagerly awaiting re-enlightenment
aruna
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Tony: Well, you may not like the definition Sun uses, but that doesn't change the fact that it is the definition Sun uses. To be clear, this isn't just an isolated accidental usage of "variable" in 15.28; there are numerous other places in the JLS which support the idea that constant fields and local variables are considered variables. And there's nothing in the JLS which supports the idea that constants and variables are exclusive. (Other than, the fact that they used two words that, admittedly, sound exclusive, and are used as such in other contexts.)

Aruna: my answer is yes, true. (Again.) Tony's may be different. Personally I recommend that if you're taking Sun's certification for the Java programming language, you use the definitions used by Sun in the specification of the Java programming language. Regardless, in the actual exam, they tend to not ask questions that depend on understanding definitions; instead, they usually show you some code, and ask you what it will do. So (in all likelihood) you won't be asked whether an inner (nonstatic) class can "have" a static "variable" (whatever we may think those words mean). You might be shown an inner class that inherits a static variable. Or you might be shown an inner class that attempts to declare a static variable, either with a constant initializer, or not. And in each case, you would be expected to figure out whether the class could compile and run, and if it did, what would the output be. If you understand that, then it really doesn't matter whether we agree on exactly what the terms mean. You can drive yourself crazy imagining various hypothetical vaguely-worded questions that might appear on the exam - and many mock exams contain such examples. But questions on the real exam are (in general) much more concrete. It's not worth worrying too much about any one question you see on a mock somewhere.
Aruna Agrawal
Ranch Hand

Joined: Jan 27, 2005
Posts: 66
Thanks Jim for your final reply.. i will follow your suggestion..
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
Not only do I not like it - it is blatantly incorrect - nothing to do with what I like. Sure, Sun uses the wrong terminology in other parts of the JLS that suggest that a constant is a variable, and they do the opposite too.

Sun is not an authoritative source of these definitions (and neither am I), they are well defined in many other, more accurate texts. The contradictory text called the "Java Language Specification Second Edition" is:
a) not the definitive source of terminology
b) contradictory, therefore, even if it did have some substance in the definition of terminology, its credibility is severely undermined due to its lack of accuracy.

In any case, I don't think any of this is examined in SCJP anyway.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[Tony]: Sure, Sun uses the wrong terminology in other parts of the JLS that suggest that a constant is a variable,

It's more than a suggestion - it's part of their definition of a constant. Which has no weight outside Java, and we'll just have to disagree as to how much weight it has for the rest of the Java world in general. But within the context of the JLS iteself at least, this is the definition.

and they do the opposite too.

Got an example? (Specifically, regarding constants vs. variables?)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: final static int i =0 Whizlabs says i is variable and i feel it is a constant
 
Similar Threads
switch case
static final with object handle
When is a String a constant?
java round-up game #223
static and final