This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes possible error in the book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "possible error in the book" Watch "possible error in the book" New topic

possible error in the book

Thomas Thomas

Joined: Jun 17, 2003
Posts: 28
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) {
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...
Thomas C Thomas
Vinay Gangoli

Joined: Jun 13, 2003
Posts: 22
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) {

Hope that is what u were looking for

SCJP 1.4<br />" Somedays ur the dog...other days ur the water hydrant "
Dirk Schreckmann

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

Joined: Jun 21, 2003
Posts: 1
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 ]
I agree. Here's the link:
subject: possible error in the book