File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static void main

 
Matt Fyffe
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, recently I've been trying to create applications and thus, use public static void main... however, a lot of it is really bothering me. First of all, how can I use ActionListener and the likes with static void main. When I tried, it would say non-static variable this can not be referenced in a static context (button.addActionListener(this));
I looked at a friends code and it seemed he used both public static void main and public void init. I tried to do this myself but init never seems to be called. So my question is, when i use public static void main, how can I get out of being static everywhere?
 
Michael Morris
Ranch Hand
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to create an object inside the main method, usually an instance of the class in which main is a member. For example:
 
Matt Fyffe
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah... thanks a lot... I'll have to try that out
 
James Clinton
Ranch Hand
Posts: 190
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Matt
With regards to your friends code, if he is developing an Application the init() methods would never be called.
About Main and Static:
The main method must be declared static as this means that it doesnt belong to the class and can therefore be invoked without the class being constructed first.
To use listeners:
Follow Michael's example above. And in the foo class implement ActionListener and the actionPerformed method as required.

Hope that helps
/ James
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic