• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

"this "of inner class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in situation of non-static,if i am in inside of an instance of inner class, how can get "this" of outer class?
class OuterOne{
int i2 = 0;
class InnerOne{
int i = 0;
void amethod(){
System.out.print("i of InnerOne = "+i);
System.out.print("i2 of OuterOne = "+this.i2);//is this correct? i
}//end of amethod
}//end of inner
public static void main(String[]a){
OuterOne.InnerOne j = new OUterOne().new InnerONe();
j.amethod();
}//end of main
}//end of outer
where can i get reference variable of OutOne object?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
First there are some typos in yr code.
We can directly access the variables of an outer class
from the inner class. In that situation we
do not require use of 'this'

Originally posted by nan s:
in situation of non-static,if i am in inside of an instance of inner class, how can get "this" of outer class?
class OuterOne{
int i2 = 0;
class InnerOne{
int i = 0;
void amethod(){
System.out.print("i of InnerOne = "+i);
System.out.print("i2 of OuterOne = "+this.i2);//is this correct? i
}//end of amethod
}//end of inner
public static void main(String[]a){
OuterOne.InnerOne j = new OuterOne().new InnerOne();
j.amethod();
}//end of main
}//end of outer
where can i get reference variable of OutOne object?


 
Ranch Hand
Posts: 318
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you use <enclosing class name>.this
in your case OuterOne.this
 
There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic