File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Foo6 compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Foo6 compilation" Watch "Foo6 compilation" New topic
Author

Foo6 compilation

toshit kolhe
Greenhorn

Joined: Jan 22, 2012
Posts: 4
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

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?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
toshit kolhe
Greenhorn

Joined: Jan 22, 2012
Posts: 4
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

Joined: Aug 05, 2005
Posts: 3168
    
  10
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.


Joanne
toshit kolhe
Greenhorn

Joined: Jan 22, 2012
Posts: 4
thanks joanne
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36478
    
  16
. . . and welcome to the Ranch
Chandraprakash Sarathe
Greenhorn

Joined: Jan 21, 2012
Posts: 16
You will get the error when you call go() in this way :-



Chandraprakash Sarathe
http://javaved.blogspot.com
toshit kolhe
Greenhorn

Joined: Jan 22, 2012
Posts: 4
got it chandrakant
 
wood burning stoves
 
subject: Foo6 compilation
 
Similar Threads
Static initializer order
static doubt
Question regarding the flow of execution with static blocks
Sharpen your pencil, chapter numbers and statics
Daught