• Post Reply Bookmark Topic Watch Topic
  • New Topic

inner class problem  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi everyone
My question is
why in the Initializer block I need to give
Outerclassname.this.member( #1) if i need to access members of
Outer object but in case of any method we access directly
outer class members by this.member (#2) iff that member isn't present in
inner class

while instantiation of inner class I have instantiated using reference to outer
object so I should be able to invoke outer members directly using this from
initializer block just like i do in method.....


thanks in advance
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a real hard time reading your code, mostly because spacing and organization made it unclear what methods went with what class. So I am posting it again formatted and organized more clear.



Sawan Mishra wrote:My question is why in the Initializer block I need to give Outerclassname.this.member( #1) if i need to access members of Outer object but in case of any method we access directly outer class members by this.member (#2)


1) why do you think you have to reference the member with Outerclassname.this.member? Have you tried without it?
2) I don't see in #2 where you use this.
3) Do you understand the difference between the location of #1 and #2? What would this refer to in #1? What would this refer to in #2?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start by posting the code with correct indentation, empty lines after successive methods, etc. Then you can see which code belongs to the inner Engine class and which to the Car class.

Code belonging to the outer class assumes that this means its own class, and code in the inner class assumes that this means its own class, too, but this time the Engine class. To access members of the surrounding class you have to write Car.this.
Yes, I know you know that already. But you are getting confused because you cannot see which code is part of which class. I copied the entire code and formatted it so I could actually read it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!