• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

doubt in Inner class...?

 
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
source: www.danchisholm.net



answer is: Prints: A1B0A0B1A0B2

can you explain what is happening at line-3?
[ December 11, 2008: Message edited by: Ganeshkumar cheekati ]
 
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ganesh you have asked about line 2 and you have made line 3 bold?? Anyways I will answer about line 2. As you might know, you need an instance of the outer class to create an instance of non-static inner class. So you are creating an instance of the inner class B, using a new object of the enclosing class i.e. A. If you are not familiar with the object.new syntax, then you must read about it a bit. You can refer to my blog too whose link is in my signature...
 
Ganeshkumar cheekati
Ranch Hand
Posts: 362
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry ankit...

i am asking about line-3 in the above code...

yeah i posted another one which is different than this code...
[ December 11, 2008: Message edited by: Ganeshkumar cheekati ]
 
Ankit Garg
Sheriff
Posts: 9709
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ganesh! I didn't see the other post with caution. It is indeed different.

You have to use an outer class instance to create an instance of the inner class. Every non-static method has a instance of the associated class which can be accessed using the this keyword. Since the class B is inside the class A, and this in method m2 refers to an instance of class A, so you can use the this to create an instance of class B. Let me change the code a bit



Now it would be clearer to you. Also the method m3 also uses this to create an instance of the inner class. The method m3 is equivalent to this code

 
reply
    Bookmark Topic Watch Topic
  • New Topic