Results 1 to 4 of 4
Like Tree2Likes
  • 1 Post By JosAH
  • 1 Post By kjkrum

Thread: Use of Static Keyword !

  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    114
    Rep Power
    0

    Default Use of Static Keyword !

    I understand that if we use static key word in front of a method then we don't have to instantiate a particular instance of the class. For example: main() method.

    1) Is there any other example besides main() method which demonstrated this functionality?

    2) Why do we use static keyword in front of a variable then?

    Please explain.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Use of Static Keyword !

    Quote Originally Posted by Jack_Tauson_Sr View Post
    I understand that if we use static key word in front of a method then we don't have to instantiate a particular instance of the class. For example: main() method.

    1) Is there any other example besides main() method which demonstrated this functionality?

    2) Why do we use static keyword in front of a variable then?

    Please explain.
    Static methods may need variables out of the scope of those methods. They can't be instance variables (there may not be an instance) so these variables have to be static. Everywhere where you only need functionality, that functionality may be implemented by a static method. Prefer to keep those static methods to a minimum because they can be a sign of pure procedural design and programmiing (nothing wrong with that, but if you want to use Pascal and its compadres, you know where to find them).

    kind regards,

    Jos
    steveeveryjavajob likes this.
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,963
    Rep Power
    8

    Default Re: Use of Static Keyword !

    I agree with everything Jos said.

    An example of a static variable that you probably use is the System.out variable. The out variable is a static PrintStream instance in the System class.

    For examples of static methods, check out the Collections class or the Math class.

    There are a ton of other places static variables and methods are used, but those are the ones you might see most frequently.

    Note: the static keyword should NOT be used as an easy way to pass information between instances.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Use of Static Keyword !

    The static keyword is also used in a static initializer. It runs once when the class is loaded. It's useful for initializing static fields that can't be initialized with a simple assignment. For example:

    Java Code:
    class MyClass {
        private static List<String> strings = new LinkedList<String>();
        static {
            strings.add("one");
            strings.add("two");
            strings.add("five");
        }
    }
    steveeveryjavajob likes this.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Can the class be instantiated with the static keyword ?
    By kashyapkeshav in forum New To Java
    Replies: 2
    Last Post: 11-04-2012, 01:57 PM
  2. static keyword
    By lala in forum New To Java
    Replies: 19
    Last Post: 01-11-2011, 08:06 PM
  3. use of static keyword
    By venkatallu in forum New To Java
    Replies: 2
    Last Post: 06-25-2009, 07:53 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
  •