• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Inner and outer class instances

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have alittle confusion regarding the instances of inner and outer classes.The Book of Robert Heller states
"There must NORMALLY be a pre-existing instances of the outer class acting as context"
while Java Notes say
"An instance of a non-static inner class can exist ONLY with an instance of its enclosing class. So it always has to be created within a context of an outer instance."
So, is it really a case of ONLY or it is a case of NORMALLY.
Thanx in Advance.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fawad,

Instance of Enclosing class is a must for creating an instance of Inner Class.
 
Fawad Khan
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But, when i make instance of the inner class without making the instance of the outer class, it compiles fine. So, is this a runtime error only ?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Fawad
u r right. following is an example: the line marked with *** compiles ok. but gives a runtime error...the error message is a huge multiline crapola!
is this proof of the fact that u can not instantiate an inner w/o an outer?
or
that the line i added makes no sense!
regards
--------------------------------------
public class OuterOne
{
private int x;
public class InnerOne
{
private int y;
public int z=100;
***InnerOne ione=new InnerOne();// compiles ok, but gives a runtime error
public void innerMethod()
{
System.out.println("enc x is:"+x);
System.out.println("y is:"+y);
}
}
public void outerMethod()
{
System.out.println("x is:"+x);
}
public void makeInner()
{
InnerOne anInner=new InnerOne();
anInner.innerMethod();
}
}
---------------------------------------------
 
Run away! Run away! Here, take this tiny ad with you:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic