This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization 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
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Inner Class question..  RSS feed

 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is some Code:



My question is that when I put the Class : MyInner in another file in the same package, then why doesnt it get recognized in the Class MyOuter? I have made the font bold where it throws error.
[ August 14, 2008: Message edited by: Somnath Paul ]
 
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can not create instance of innerclass from different class(other than outer class)
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by seetharaman venkatasamy:
you can not create instance of innerclass from different class(other than outer class)



But I am creating the instance of inner class in the MyOuter only !



And since the access is default package, it should recognize the inner class..
Am I wrong? Please correct me if I am, I am very new to Java.

[ August 14, 2008: Message edited by: Somnath Paul ]
[ August 14, 2008: Message edited by: Somnath Paul ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Once you move the MyInner class to another file it is no longer an inner class of MyOuter. You would have to use it like so:

 
Sheriff
Posts: 21719
102
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by seetharaman venkatasamy:
you can not create instance of innerclass from different class(other than outer class)


Yes you can, as long as a) it's visible, and b) you have a reference to an enclosing outer class.

Somnath,

the problem in your code is that MyInner is NOT an inner class - it's a toplevel class instead. If you want it to be an inner class, you should put the entire class definition inside the class definition of MyOuter. Basically, just move the last } of your outer class to after the inner class (and then fix indentation).

You might want to know that, from an inner class, you can get a reference to its outer class by calling MyOuter.this
[ August 14, 2008: Message edited by: Rob Prime ]
 
She'll be back. I'm just gonna wait here. With this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!