• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

inner class

 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can we instantiate inner class in static method of outer class as we do in instance method ?

Thanks
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give it a try!
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jelle Klap wrote:Give it a try!


this reference of outer class doesn't available in static methods.Please, tell me.
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It’s all in the Java Tutorials.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

---------- 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
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What did you find in the Java tutorials link?
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks more promising. Now try that in a static method of the outer class.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49841
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Success!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic