Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Foo6 compilation

 
toshit kolhe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is small code from sharpen your pencil exercise in the head first book


this code got compiled but i was expecting the error like "non static variable x cannot be referenced from a static context"

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64858
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It would likely be easier to see if the code were properly indented, but there are two variables named x in the class. To which does the line in question refer?
 
toshit kolhe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear sir,
i think think that the initialised int x = 24; is an instance variable and cannot be called by the static method void go(). i thought it would not compile
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toshit kolhe wrote:i think think that the initialised int x = 24; is an instance variable and cannot be called by the static method void go().

That's correct, but as Bear says, you have two variables called x, so have you considered it might be the other x that is being referenced in the go method.
 
toshit kolhe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks joanne
 
Campbell Ritchie
Sheriff
Pie
Posts: 48981
60
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
. . . and welcome to the Ranch
 
Chandraprakash Sarathe
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will get the error when you call go() in this way :-

 
toshit kolhe
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it chandrakant
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic