File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes defining a non static method or class inside main? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "defining a non static method or class inside main?" Watch "defining a non static method or class inside main?" New topic
Author

defining a non static method or class inside main?

sahar eb
Ranch Hand

Joined: May 06, 2009
Posts: 38
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

Joined: Jan 28, 2003
Posts: 4167
    
  21

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:



Steve
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11155
    
  16

It's not NetBeans that won't let you do that...it's Java.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
sahar eb
Ranch Hand

Joined: May 06, 2009
Posts: 38
problemo solved! thanks!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: defining a non static method or class inside main?
 
Similar Threads
doubts in setDate(i,date,cal) API of preparedStatement
mysql connectivity with jboss
ejbLoad is not getting called
Newbie having problems with StrutsMocktestCase
DBUtility for closing connections