| Author |
possible error in the book
|
Thomas Thomas
Greenhorn
Joined: Jun 17, 2003
Posts: 28
|
|
hi, i don't i am placing this question in the right place. In the certification book by kathy. Page 200 Q.13 class Test{ static int s; // more code void twice (int x) { x=x*2; s=x; } } ans to this question is shown as if there was no keyword static .. Or is it something that i didn't understand about the question... Thanks Thomas C Thomas
|
 |
Vinay Gangoli
Greenhorn
Joined: Jun 13, 2003
Posts: 22
|
|
Hi, a static variable declared ina class X can be accessed inside class X without using the classname and dot operator.hence calling the method twice assigns the static int s the value 2*x. class Test{ static int s; // more code void twice (int x) { x=x*2; s=x; } } Hope that is what u were looking for
|
SCJP 1.4<br />" Somedays ur the dog...other days ur the water hydrant "
|
 |
Dirk Schreckmann
Sheriff
Joined: Dec 10, 2001
Posts: 7023
|
|
Thomas, what is the question and what is the answer? I'm moving this to the Programmer Certification forum...
|
[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
|
 |
Wilson Redwood
Greenhorn
Joined: Jun 21, 2003
Posts: 1
|
|
Thomas, I think you may be getting static mixed up with final, which was what i did when i first saw this question, expecting the answer to be compilation fails. Basically, static allows the variable or method to be reached without instantiating the class. In this question, it is basically used to throw you off, test if you know the proper use of static. static still allows the int to be changed in the method twice(int x). If it had been: final static int s; then s would not be allowed to be reassigned a value. [ June 21, 2003: Message edited by: Wilson Redwood ]
|
 |
 |
|
|
subject: possible error in the book
|
|
|