aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class problem" Watch "inner class problem" New topic
Author

inner class problem

thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56


1)In here I want to know to get the output as "Body" why we have to apply Body.this.name?please explain this.
2)what code we have to type in getPName () method to get Heart as a output.


Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5


1)In here I want to know to get the output as "Body" why we have to apply Body.this.name?please explain this.



This is because you want to refer to Body's name. Not Heart's.




2)what code we have to type in getPName () method to get Heart as a output.


This should be simple for you to get. What variable name can you use to make sure the 'name' reference in Heart is made use of ? What have you tried so far ?


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

1)In here I want to know to get the output as "Body" why we have to apply Body.this.name?please explain this.

we want to refer to the outer class isn't it?
now when we are accessing inner class, then the this reference refers the inner class object
so to access the outer class object we have to write OuterClass.this

2)what code we have to type in getPName () method to get Heart as a output.

Now we want to the member of inner class isn't it?
hence we simply use the this reference

the code is as follows

Hope this helps
hth
happy preparation

[edit] corrected the mistake in the code as per Deepak's suggestion [/edit]

@ Deepak

is this good way to correct the mistakes and editing the posts
I mean writing the edit tag (I have seen this from many good people)
am I contributing neatly?

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

Prasad Kharkar wrote:
1)In here I want to know to get the output as "Body" why we have to apply Body.this.name?please explain this.

we want to refer to the outer class isn't it?
now when we are accessing inner class, then the this reference refers the inner class object
so to access the outer class object we have to write OuterClass.this

2)what code we have to type in getPName () method to get Heart as a output.

Now we want to the member of inner class isn't it?
hence we simply use the this reference

the code is as follows

Hope this helps
hth
happy preparation


Prasad your code still uses the outer Body class
thejaka samarakoon
Ranch Hand

Joined: Jul 20, 2010
Posts: 56
thanks a lot Prasad
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6662
    
    5

@ Deepak

is this good way to correct the mistakes and editing the posts
I mean writing the edit tag (I have seen this from many good people)
am I contributing neatly?


As long as everyone understands the context things are cool.
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

@ Deepak
yes sir
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class problem