Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

possible error in the book

 
Thomas Thomas
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas, what is the question and what is the answer?
I'm moving this to the Programmer Certification forum...
 
Wilson Redwood
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic