Results 1 to 2 of 2
  1. #1
    Digital Larry is offline Member
    Join Date
    Mar 2013
    Rep Power

    Default Why should createAndShowGUI be declared static?

    I just noticed this in some of the Oracle Java examples and am a little confused.

    createAndSHowGUI() is only ever called once typically, and has no methods defined internally, so I don't see what benefit it serves to declare it static.

    I read through this:

    Static Methods

    but it's not clear what aspects of a static declaration are important to createAndShowGUI().

    I've recently been combing through the code in my one big project and making methods "un-static" as much as possible since I don't generally need them to be standalone function libraries. Just about everything I'm doing, I am doing on an instance of an object.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: Why should createAndShowGUI be declared static?

    Typically static methods are used when they are not processing object state directly (i.e. other than thru the passed arguments). In the case you cite, the createAndShowGui() method is almost always invoked from main. Since main is a static method, it makes it easier (and imho, cleaner) to invoke the that method inside the EDT as opposed to instantiating the enclosing class and then dereferencing an instance method to achieve the same goal.

    Last edited by jim829; 10-25-2015 at 07:47 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 2
    Last Post: 06-05-2014, 03:26 PM
  2. Replies: 3
    Last Post: 05-31-2012, 07:56 PM
  3. Replies: 5
    Last Post: 10-06-2008, 12:11 AM
  4. Replies: 1
    Last Post: 08-07-2007, 05:05 AM
  5. Replies: 1
    Last Post: 08-01-2007, 09:25 PM

Posting Permissions

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