Results 1 to 7 of 7
  1. #1
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default Static methods or individual classes?

    My swing application file has quite a few static methods which I plan to remove by creating a separate class for each static method in the same folder and then use the methods of these classes in my application file. Does it matter which approach I take? If yes, which approach is better - using a lot of static methods in my app OR writing a class for each static method?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Why would you write a class for each static method? What do these methods do?

    It matters which approach you take. But only to you. We can't really tell you what approach is best, especially without seeing any code (no, that doesn't mean paste all your code here). Try an approach. Does it work?

  3. #3
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    The app structure looks something like this:

    public class MyApp
    {
    static JMenuBar createMenuBar()
    {
    // create menu bar
    }
    static JToolBar createToolBar()
    {
    // create toolbar
    }
    static JLabel createStatusBar()
    {
    // create status bar
    }

    ActionListener ac = new ActionListener()
    {
    // handling swing events...
    };

    JFrame myFrame = new JFrame();
    myFrame.setVisible(true);
    ...
    ...
    }

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    I guess the question is, why are these methods static in the first place?

    Dollar bet: You did that to resolve "non-static XYZ referenced from static context" exceptions, didn't you? DIDN'T YOU. :p

  5. #5
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

    Default

    er... yes. Is that an incorrect methodology?

  6. #6
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default

    Yes and no. It's common for a "newbie" (or even a mid-level or slightly experienced) programmer to use the "quick fix" for that kind of Exception without actually understanding what the problem is.

    I am betting you actually have something like this:

    Java Code:
    public class MyApp
    {
       static JMenuBar createMenuBar(){
          // create menu bar
       }
       static JToolBar createToolBar(){
          // create toolbar
       }
       static JLabel createStatusBar(){
          // create status bar
       }
    
       public static void main(String [] args){
    
          JFrame frame = new JFrame();
    
          //pseudocode, but you get the point
          frame.add(MyApp.createMenuBar());
          frame.add(MyApp.createToolBar());
          frame.add(MyApp.createStatusBar());
       }
    }
    When really you should have something more like:

    Java Code:
    public class MyApp
    {
       public JMenuBar createMenuBar(){
          // create menu bar
       }
       public JToolBar createToolBar(){
          // create toolbar
       }
       public JLabel createStatusBar(){
          // create status bar
       }
    
       public static void main(String [] args){
    
          JFrame frame = new JFrame();
     
          MyApp appInstance = new MyApp();
    
          //pseudocode, but you get the point
          frame.add(appInstance.createMenuBar());
          frame.add(appInstance.createToolBar());
          frame.add(appInstance.createStatusBar());
       }
    }
    That's just guessing at what you're doing, but I'm hoping it demonstrates the point.
    Last edited by KevinWorkman; 12-01-2010 at 03:53 PM.

  7. #7
    nik_meback is offline Member
    Join Date
    Dec 2010
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  2. Call Static Methods Outside Classes
    By Insomniac Riot in forum New To Java
    Replies: 4
    Last Post: 05-11-2010, 10:03 PM
  3. static vs. non-static nested classes
    By rinke in forum Advanced Java
    Replies: 8
    Last Post: 06-30-2009, 07:15 AM
  4. Recursion with static and non static methods
    By sh4dyPT in forum New To Java
    Replies: 14
    Last Post: 03-27-2009, 06:56 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
  •