This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Learn Java with Math: Using Fun Projects and Games and have Ron Dai on-line!
See this thread for details.
Win a copy of Learn Java with Math: Using Fun Projects and Games this week in the Beginning Java forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Inner Class

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I found this code in the Getting in Touch with your Inner Class
tutorial on this site, but I think you cannot create an inner class object as specified in the code
Inner i = o.new Inner();
Shouldn't it be
Outer.Inner i = o.new Inner();
??
thankx
dre

class Outer
{
private int size ;
private String thoughts = "My outer thoughts";
class Inner
{
String innerThoughts = "My inner thoughts";
void doStuff()
{
// inner object has its own "this"
System.out.println( innerThoughts );
// and it also has a kind of "outer this"
// even for private data of outer class
System.out.println(thoughts);
}
}
}
class TestMe
{
public static void main( String args[] )
{
// instantiate me, the outer object
Outer o = new Outer();
// Inner i = new Inner();
// NO! Can't instantiate Inner by itself!
Inner i = o.new Inner();
// now I have my special inner object
i.doStuff();
// OK to call methods on inner object
}
}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right - good catch. I'm moving this to the "Java Ranch" topic to be sure Paul sees it, as he's the one who could fix the error. (Hi Paul!) Thanks.
 
Bras cause cancer. And tiny ads:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!