• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Inner class and this ?

 
Ranch Hand
Posts: 417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]


 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To ident code read here:
http://www.javaranch.com/ubb/faq.html#q4
 
Then YOU must do the pig's work! Read this tiny ad. READ IT!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic