| Author |
inner class
|
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Can we instantiate inner class in static method of outer class as we do in instance method ?
Thanks
|
OCA7
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1404
|
|
|
Give it a try!
|
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Jelle Klap wrote:Give it a try!
this reference of outer class doesn't available in static methods.Please, tell me.
|
 |
Jelle Klap
Bartender
Joined: Mar 10, 2008
Posts: 1404
|
|
But this is trivial to just try out and see for yourself.
Either it compiles, or it doesn't. If you don't understand why it does or does not compile, maybe we can help with that.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
It’s all in the Java Tutorials.
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
---------- Compile ----------
MyOuter1.java:4: error: non-static variable this cannot be referenced from a static context
MyInner1 i=new MyInner1();
^
1 error
Output completed (1 sec consumed) - Normal Termination
I tried, it is not working.Now how to instantiate inner class here ?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
What did you find in the Java tutorials link?
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Campbell Ritchie wrote:What did you find in the Java tutorials link?
It was same as i mentioned above this reference doesn't available in static methods.
An instance of InnerClass can exist only within an instance of OuterClass..
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3026
|
|
ankita modi. wrote:
Campbell Ritchie wrote:What did you find in the Java tutorials link?
It was same as i mentioned above this reference doesn't available in static methods.
An instance of InnerClass can exist only within an instance of OuterClass..
Great, you have your answer then. You originally asked:
ankita modi. wrote:Can we instantiate inner class in static method of outer class as we do in instance method ?
and through both trial and reference you know the answer.
|
Steve
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Steve Luke wrote:
ankita modi. wrote:
Campbell Ritchie wrote:What did you find in the Java tutorials link?
It was same as i mentioned above this reference doesn't available in static methods.
An instance of InnerClass can exist only within an instance of OuterClass..
Great, you have your answer then. You originally asked:
ankita modi. wrote:Can we instantiate inner class in static method of outer class as we do in instance method ?
and through both trial and reference you know the answer.
No,that is not answer of my question.There could be a good answer of my question.Anyways i do post here
//inner class instantiate in static method of outer class
MyOuter.MyInner obj=new MyOuter().new MyInner();
-hth
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
|
That looks more promising. Now try that in a static method of the outer class.
|
 |
Enkita mody
Ranch Hand
Joined: Aug 06, 2012
Posts: 333
|
|
Campbell Ritchie wrote:That looks more promising. Now try that in a static method of the outer class.
---------- Run ----------
from inner
Output completed (0 sec consumed) - Normal Termination
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
Success!
|
 |
 |
|
|
subject: inner class
|
|
|