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 Inner class and this ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Inner class and this ?" Watch "Inner class and this ?" New topic
Author

Inner class and this ?

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
Class B {protected int x =2;}
Class A {private int x = 3;
Class C extends B {
//private int w = x;
private int y = this.x;
private int z = A.this.x; }
two questions here:
first i have never seen this one A.this.x
what is its exact syntax is it (A).this.x or A.(this.x)
or neither. is it a specical one ?
and second why is int w = x illegal (or unambiguous)
and what exactly does y = this.x refer to ? which x ?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
With this code you can see the diference between this and Outer.this within a method in an Inner class.


SCJP2. Please Indent your code using UBB Code
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
yes it is starting to make things clearer.
but what exactly..., or mean how do we read this
A.this.x
is it A.(this.x)
or (A.this).x
because till now i have seen Object.variable
or Class_name.Variable syntax.
have'nt quite seen with two dots. or is it
pretty common and i just have'nt had the experience
of more programming !!
Originally posted by Jose Botella:
[B]With this code you can see the diference between this and Outer.this within a method in an Inner class.
[/B]

anand raman
Ranch Hand

Joined: Jun 06, 2001
Posts: 66
hi
let me see, if I can make things a little more clear. All non static inner classes have a reference to the enclosing class which can be accessed by saying <EnclosingClass>.this.<variableName> or <EnclosingClass>.this.method()..
However this is not always necessary as the reference to the enclosing class is always present.
Hope this helps.
Anand
mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
ok that's what i was asking about the
A.this.x
"class_name".this."var_or_method"
this is fine ?
Originally posted by anand raman:
hi
let me see, if I can make things a little more clear. All non static inner classes have a reference to the enclosing class which can be accessed by saying <EnclosingClass>.this.<variableName> or <EnclosingClass>.this.method()..
However this is not always necessary as the reference to the enclosing class is always present.
Hope this helps.
Anand

Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
public class Outer
{
int Age;
}
public class Wrapper
{
private int Age = 10;
public Wrapper()
{
final int Age = 20;//need access
class Inner extends Outer
{
private int Age = 30;
public Inner()
{
System.out.println("Wrapper Age = " + Wrapper.this.Age);
System.out.println("Wrapper Construct Age = " + ???.Age);//the variable name
System.out.println("Inner Age = " + Age);
System.out.println("Outer Age = " + super.Age);
}
}
Inner myInner = new Inner();
}

public static void main(String[] args)
{
Wrapper myApp = new Wrapper();
}
}
If there are four variables with the same name but in a different location, how do I access to the Age in the Wrapper Constructor? Thanks!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Mark
Yes it is fine.
to Vinny
"final int Age = 20;//need access"
has shadowed any other declaration accessible via a single Age

Please ident your code. Thanks
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
Jose, thanks for your explanation.
However, one more simple question, how can I indent my code in this forum? I just copy and paste the code to the Reply box, however, all the tabs are disappeared. I hate to see un-indented codes too! Please help!
Kelley Koszegi
Greenhorn

Joined: Aug 20, 2001
Posts: 27
Use the <code> & </code> tags around your code snippets. Then they will be properly formatted.
--Kelley
Originally posted by Vinny Chun:
Jose, thanks for your explanation.
However, one more simple question, how can I indent my code in this forum? I just copy and paste the code to the Reply box, however, all the tabs are disappeared. I hate to see un-indented codes too! Please help!


Kelley Koszegi<br />Sun Certified Java Programmer
Vinny Chun
Greenhorn

Joined: Dec 06, 2001
Posts: 24
Testing the indent
<&>class Question
{
public static void main(String[] args)
}
</&>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
To ident code read here:
http://www.javaranch.com/ubb/faq.html#q4
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Inner class and this ?
 
Similar Threads
how to call object in another class?
explain
Hierarch of non-static inner class
abstract class instantiation
Anonymous inner class