• 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

Instantiating Inner Classes

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
theres a method to instantiate inner class elsewhere in this site which says Inner class in Outer class can be instantiated as
"Inner i = new Outer().new Inner();". But i could not do it this way as compiler could not find the Inner class.
Any body listning!!!
 
Ranch Hand
Posts: 396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cud u post ur code here
 
Alexandre Bellezi Jos�
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer
{
private int size ;
private String thoughts = "My outer thoughts";
class Inner {String innerThoughts = "My inner thoughts";
void doStuff()
{System.out.println(innerThoughts ); System.out.println(thoughts);}}}
public class TestMe
{ public static void main( String args [] )
{ Outer o = new Outer();
Inner j = new Outer().new Inner();
Outer.Inner i = o.new Inner();
i.doStuff();
j.doStuff();
}}

 
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here it is important to know that when you attempt to instantiate an inner class without prefixing the instance of an outer class then implied prefix this. is assumed. This will not work here because you are trying to instantiate an inner class from some other class. That is why it is necessary to give the outer context (full path).
Make the following change in your code and it will compile.
class Outer {
private int size ;
private String thoughts = "My outer thoughts";
class Inner {String innerThoughts = "My inner thoughts";
void doStuff()
{System.out.println(innerThoughts ); System.out.println(thoughts);}}}
public class TestMe
{ public static void main( String args [] )
{ Outer o = new Outer();
Outer.Inner j = new Outer().new Inner();
Outer.Inner i = o.new Inner();
i.doStuff();
j.doStuff();
}}
Hope this helps !!
Regards,
Milind


[This message has been edited by Milind Kulkarni (edited June 02, 2000).]
 
Alexandre Bellezi Jos�
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks milind the code compiled well.actually i was trying the same concept but with a little difference.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so would:
Outer.Inner j = o.new Inner();
work just as well as:
Outer.Inner j = new Outer().new Inner();
? (I don't have a compiler on the computer I'm using right now to test it out.)
[This message has been edited by n_sky (edited October 03, 2001).]
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Too many naming violators here. Where are our bartenders?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Most of this thread was posted over a year ago, before we started enforcing the naming policies. Only "n_sky" has posted recently. n_sky, please read our user name policy and re-register with a valid name. Thanks...
 
Ranch Hand
Posts: 1170
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a more appropriate solution is below.

Originally posted by abj:
class Outer
{
private int size ;
private String thoughts = "My outer thoughts";
class Inner {String innerThoughts = "My inner thoughts";
void doStuff()
{System.out.println(innerThoughts ); System.out.println(thoughts);}}}
public class TestMe
{ public static void main( String args [] )
{ Outer o = new Outer();
Inner j = new Inner();
Outer.Inner i = o.new Inner();
i.doStuff();
j.doStuff();
}}


Note that the outer classes are different for these 2 Inner classes. The first is effectively
Inner j = this.new Inner();

[This message has been edited by CL Gilbert (edited October 05, 2001).]
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic