Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to call inner class method from static main() method - No static class

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have an inner class(It is not a static class, just plain inner class) inside an outer class given below:



I would want to call display() method from static void main() method.
Coditions:
1. Do not insert/delete any code written above.
2. We have a flexibility to add any number of lines in main() method

How can we do that?

--
Thanks,
Manoj Kumar
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Homework?

I'll give you a hint: an instance of a non-static inner class can only exist in the context of an instance of the containing class.

edit I'll also give you a word of advice: By convention, class names start with an uppercase letter.
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I tried adding these two statements in main() method. But, it is giving me compile time error. What am I doing wrong??

--
Thanks,
Manoj Kumar
 
Ranch Hand
Posts: 633
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
create inner class object as
then call inner class method
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much
 
Darryl Burke
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you need to retain references to both the instances of outerClass and innerClass:Note that since the code is within the class scope of outerClass, you don't need to fully qualify the name of innerClass. Pramod's code can also be written as
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic