Win a copy of liveProject: Build an ML Recommender System this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Inner class doubt

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers!



ouput --> Hello
Default

Pls explain why the output is NOT -->
Default
Default

Appreciate your help!

Thanks
Neha Monga
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

neha

In inner = o.new In();

When the jvm executes this statement, the default constructor of the Out will be executed because in extends Out.There you are setting the id="Default",And in the inner class if you simply say id ,means this.id.Because In extends Out the id is inherited to the subclass because it is not declared as private.

This is a specaial syntax to get refernce to the top level class.
System.out.println(Out.this.id);//refers to

Out o = new Out("Hello");//line 1
In this the default constructor won't run.

Thanks
Anil Kumar

[ May 31, 2007: Message edited by: anil kumar ]
[ May 31, 2007: Message edited by: anil kumar ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Neha,

Adding to what Anil said:

If you get how many times the no-arg constructor of class Out is called,
you can make yourself doubt clear.

 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic