Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Inner class and this ?

 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With this code you can see the diference between this and Outer.this within a method in an Inner class.
 
mark stone
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 417
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Vinny Chun
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Testing the indent
<&>class Question
{
public static void main(String[] args)
}
</&>
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To ident code read here:
http://www.javaranch.com/ubb/faq.html#q4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic