Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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
Author

Usage of this with inner classes

Sekhar Choudary
Ranch Hand

Joined: May 03, 2008
Posts: 57
Hi,
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);
this.m2();
//#line 1 Insert here

m2();

}//m1
}//myinner

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

public class InnerThisEx{

public static void main(String[] args){

MyOuter.MyInner ob=new MyOuter().new MyInner();
ob.m1();
}//main
}//class


Now my question is ,can we insert any of the following statements at line#1 for successful compilaton.
If so, let me the reason.
1.MyOuter.this.m2();
2.MyOuter.this(m2());

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%
Working on SCWCD/OCPJWCD
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
Greenhorn

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.


Njoy
 
wood burning stoves
 
subject: Usage of this with inner classes