• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

how this executes

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class sample
{
String str = "outer String";
class inner
{
String str = " inner string";
void some()
{
System.out.println(sample.this.str);
}
}

public static void main(String a[])
{
new sample().new inner().some();
}
}
hi,
What does the statement sample.this signifies here...what does this actuallu mean ?
Please explain
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Krish,
The problem is that if you only use 'this' inside your inner class you will be printing "inner string" because that is the class variable for the inner class. So you need some way of getting to your parents class variables. Java gives you a way by letting you use the
Parent.this
statement. This tells java use the class specified first (in front of the this) to located the variable information instead of my own class. In your case, 'sample.this' tells the compiler to use the str "outer String" and give it to the print method.
Regards,
Manfred.
 
There is no "i" in denial. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic