Results 1 to 5 of 5
- 05-11-2010, 03:07 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 14
- Rep Power
- 0
Call Static Methods Outside Classes
This DNA-creation project I am making has a JMenu. The selections are:
In my main class, called "ConstructMenuActionListener.java", it holds all of the buttons for the JMenu and the Action Listener for each one of them. IN a different class called "JMenuMethods.java", it holds all of the button's actions called by the Action Listener inside of ConstructMenuActionListener.java. Them methods are static, but I have no idea how to call on them. Heres a quick part of my code, this deals with the "Translate" JMenuItem inside the "Commands" Tab.Java Code:- File - New - Load - Save - Save As... - Commands - Insert Start - Insert Codon - Insert Stop (Seperate) - Transcribe - Translate - Help - DNA Help - Loading/Saving - Commands - Other - Change Theme - Version Number - Credits
And here is the "Translate" void inside the "JMenuMethods.java":Java Code:translate.addActionListener(new ActionListener() { public void actionPerformed (ActionEvent e) { // This is where it is called. // Should I call it like this? // JMenuMethods.translate(); translate(); } });
Help would be nice.Java Code:public static void translate() { // I haven't made this yet // Action goes here }
-
Your first goal would be to make things compile. What happens when you try to call the method on the class, i.e.,
Does it compile? If so, then you know that you're probably on the right track.Java Code:JMenuMethods.translate();
The next issue is to consider whether those methods should be static or not. Most recommend to avoid using static methods unless there are definite reasons to do so, and not knowing more about the structure of your program, I'm not sure if you have definite reasons or not. Best of luck.Last edited by Fubarable; 05-11-2010 at 09:16 PM. Reason: Sigh: It's "track", not "tract".
- 05-11-2010, 05:16 PM #3
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
- 05-11-2010, 05:47 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
To me it looks like a functional (or procedural) decomposition: collect those actions nicely in a separate utility class. I would at least have made it some sort of an ActionFactory that hands out the appropriate Action but then again, those Actions are known in advance so why have a separate class for them ...
kind regards,
Jos
- 05-11-2010, 10:03 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 14
- Rep Power
- 0
Similar Threads
-
Trouble with static methods and boolean equals() methods with classes
By dreamingofgreen in forum New To JavaReplies: 8Last Post: 04-16-2012, 11:00 PM -
static vs. non-static nested classes
By rinke in forum Advanced JavaReplies: 8Last Post: 06-30-2009, 07:15 AM -
Recursion with static and non static methods
By sh4dyPT in forum New To JavaReplies: 14Last Post: 03-27-2009, 06:56 AM -
How to call methods of different classes
By adeeb in forum New To JavaReplies: 2Last Post: 06-06-2008, 06:08 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks