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

defining a non static method or class inside main?

 
sahar eb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
I have this main method which is static. netbean doesnt let me to call methods which are not static inside of the main .
it gives me error below
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - non-static method getConnection() cannot be referenced from a static context
at com.mycompany.sitemap.App.main(App.java:12)


here is my code



when I declare the methods static then its fine but I know that its not a good idea. what is the right way to call methods from static method with out making them static_ the way around?

thanks
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sahar, welcome to the Ranch!

You need to make an instance of the class, then call the method from the instance. Something like this:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12086
29
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's not NetBeans that won't let you do that...it's Java.
 
sahar eb
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
problemo solved! thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic