• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Inner classes

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method-local inner class shouldn't be instantiate outside of that method.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
 
Marshal
Posts: 77157
370
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic