Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

    Default Call Static Methods Outside Classes

    This DNA-creation project I am making has a JMenu. The selections are:

    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
    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:
    		translate.addActionListener(new ActionListener() {
    			public void actionPerformed (ActionEvent e) {
    				// This is where it is called.
    				// Should I call it like this?
    				// JMenuMethods.translate();
    				translate();
    			}
    		});
    And here is the "Translate" void inside the "JMenuMethods.java":

    Java Code:
    	public static void translate()
    	{
    		// I haven't made this yet
    		// Action goes here
    	}
    Help would be nice.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your first goal would be to make things compile. What happens when you try to call the method on the class, i.e.,
    Java Code:
    JMenuMethods.translate();
    Does it compile? If so, then you know that you're probably on the right track.

    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".

  3. #3
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    Quote Originally Posted by Fubarable View Post
    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.
    It definitely smells.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by PhHein View Post
    It definitely smells.
    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

  5. #5
    Join Date
    Mar 2010
    Posts
    14
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM
  3. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 AM
  4. How to call methods of different classes
    By adeeb in forum New To Java
    Replies: 2
    Last Post: 06-06-2008, 06:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •