This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
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

Inner classes

 
Sid Shankar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This code is from the SCJP study guide with a slight modification. The following code compiles successfully but there is NO output. I was wondering why the statement "Inside getY()" is not generated. Is it because everything inside the local method getY() is erased once out of the method? Still feels like the statement should be output.... Would appreciate any answers.

Thanks in advance.

Sid.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
method-local inner class shouldn't be instantiate outside of that method.
 
Winston Gutkowski
Bartender
Pie
Posts: 10111
56
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sid Shankar wrote:Would appreciate any answers.

Sid.

I've added code tags to your post, but the indenting is still bad. I'll leave that to you to sort out. Just use the 'Edit' icon.

Please read the UseCodeTags (←click) carefully.

Thanks

Winston
 
Tony Docherty
Bartender
Posts: 2942
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding a call to getX() so the code actually runs.
 
Sid Shankar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have instantiated the class within the method of course. It should give me something because I'm running the method. Tony, I will try calling getX() and see what happens.

Winston, thanks for the tip. Needed to know that.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sid Shankar wrote:I have instantiated the class within the method of course.

Do you think that below codes are in getX() ?, these code should be in getX() not outside of that.

InnerClass inner;
inner = new InnerClass();
inner.getY();
 
Campbell Ritchie
Sheriff
Posts: 48441
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Please note Winston’s point about indentation. Study guides are often incorrectly indented to confuse you about where a bit of code is. Sort out the indentation and you can easily see whether that code is in the method or not.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got confused because of your bad code formatting.If you had read Winston tipline then you didn't find it useful to follow it ?

and the mistake is already tony told you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic