jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "inner class" Watch "inner class" New topic
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: 1753
    
    7

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: 1753
    
    7

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: 38003
    
  22
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: 38003
    
  22
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: 4167
    
  21

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: 38003
    
  22
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: 38003
    
  22
Success!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: inner class
 
Similar Threads
Anonymous inner class
inner class
Accessing the new method in the anonymous inner class
about inner classes
Quick Question