This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
agilemanoj kumar
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
 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • 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
 
Pramod P Deore
Ranch Hand
Posts: 632
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: 5125
11
Java Netbeans IDE Opera
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic