File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes difference b/w class.this.var and ((class)this).var 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 "difference b/w class.this.var and ((class)this).var" Watch "difference b/w class.this.var and ((class)this).var" New topic
Author

difference b/w class.this.var and ((class)this).var

neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
hi,
can anyone tell me the difference between acessing hidden variable and accesing variable from outer class.
in the following program--when i access outer variable as test.this.s(in line 5) --compiles fine--but when i access the same hidden variable as ((test)this).s i get compile error

WHEN DO WE EXACTLY USE (test)this).s FORMAT (to access hidden vars) AND HOW???

public class test {
final String s="outer";
class Inner {
String s="inner";
public void m1() { System.out.println(((test)this).s); }//line 5
}//inner
public void m() { System.out.println("in outer");}//m()
public static void main(String args[]){
test o = new test();
o.new Inner().m1();
}
}
NEETA
Stefan Guilhen
Ranch Hand

Joined: Jul 31, 2004
Posts: 61
When you do you are saying the following: "take the reference to this inner class object, cast it to test and then try to access the s field". But notice that you cannot cast this to test in the code you provided, because this is a reference to the inner class and you cannot cast it to be a reference to the outer class.

When you do you are actually accessing the field s using a reference to the actual outer class object and this is fine. What you are saying when you do that is: "Take the reference to the outer class object and try to access the field s using that reference".

Hope that helps
Stefan
[ August 31, 2004: Message edited by: Stefan Guilhen ]

SCJP 1.4, SCBCD 1.3<br />IBM 141, 484, 486
neeta mathur
Greenhorn

Joined: Aug 05, 2004
Posts: 22
got it--it must be used only when there is an "is-a" relationship
Thank u stephen
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: difference b/w class.this.var and ((class)this).var