my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Usage of this with inner classes 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 "Usage of this with inner classes" Watch "Usage of this with inner classes" New topic

Usage of this with inner classes

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
source code is as follows

class MyOuter{
class MyInner{
public void m1(){
System.out.println("This class refernce:"+this);
System.out.println("Outer class refernce:"+MyOuter.this);
//#line 1 Insert here



public void m2(){ System.out.println("Outer classes method"); }

public class InnerThisEx{

public static void main(String[] args){

MyOuter.MyInner ob=new MyOuter().new MyInner();

Now my question is ,can we insert any of the following statements at line#1 for successful compilaton.
If so, let me the reason.

Thank you.
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
how about compiling & checking it ???

i think the 1st one will work but the 2nd one won't

SCJP 6 83%
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
and use code tags to make the code readable

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Bhanurekha Chintagunta

Joined: Jul 28, 2008
Posts: 4
MyOuter.this is an Object refrence to MyInner class, not method that takes
void as param. so 2) does not compile.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Usage of this with inner classes
It's not a secret anymore!