File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "possible error in the book" Watch "possible error in the book" New topic
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: possible error in the book