• 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
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

inner class

 
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we instantiate inner class in static method of outer class as we do in instance method ?

Thanks
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Give it a try!
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 73260
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It’s all in the Java Tutorials.
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73260
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What did you find in the Java tutorials link?
 
Enkita mody
Ranch Hand
Posts: 333
Firefox Browser Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..

 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73260
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 73260
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Success!
 
Well THAT's new! Comfort me, reliable tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic