Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Static methods and variables

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Static methods and variables

    Hi,

    Lately I've developed an interest in game developing and chosen libgdx as my library. So I was collecting resources and reading them. Someone gave me a link to a video (sadly I lost it with many many bookmarks), and the guy used static keyword more than I ever did.

    While I was studying his code (typing to my IDE and altering it), I started to think about static keyword.

    Static variables, I can understand. If you don't want to create an instance of a class just for a variable, you just make it static. Or if you want a variable to have the same value at any time in everywhere.
    But what about static methods?

    I went through my old codes (both practice codes and the codes I tried to make a good program) and I realized I almost never used static variables, let alone static methods.

    So, why would one decide to use static method? And, what are some cases in which I could use static keyword?


  2. #2
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: Static methods and variables

    static methods are often used for helper methods, that are stateless. E.g. a method that formats a Date into a String or builder methods, or whatever. Another classic case are the getInstance() methods from singletons.

  3. #3
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Static methods and variables

    Hmm... I guess I could use some static methods in some of my apps...

    How does this compare to usual method in terms of performance and resource consuming?

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    571
    My Mood
    Sleepy
    Thanks
    9
    Thanked 84 Times in 78 Posts

    Default Re: Static methods and variables

    Zero, nothing, nada. If that mattered, you have other severe bottlenecks. No need to worry about that, unless you plan to deploy applications on a toaster.

  5. #5
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Static methods and variables

    Thanks. That was soo clear

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,166
    My Mood
    Hungover
    Thanks
    141
    Thanked 597 Times in 512 Posts

    Default Re: Static methods and variables

    Quote Originally Posted by beer-in-box View Post
    Static variables, I can understand. If you don't want to create an instance of a class just for a variable, you just make it static. Or if you want a variable to have the same value at any time in everywhere.
    Those aren't really true. You shouldn't use static as a way to pass information between classes- what happens when you want to have multiple instances of a class? And nothing is stopping a static variable from having different values over time.

    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.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Static methods and variables

    Quote Originally Posted by beer-in-box View Post
    ...So, why would one decide to use static method?
    When the method will always be the same across any instance of the class.
    Quote Originally Posted by beer-in-box View Post
    ...And, what are some cases in which I could use static keyword?
    Take a "Temperature" class for example. The conversion from fahrenheit to celsius is always the same.

  8. #8
    Member
    Join Date
    Jun 2011
    Posts
    94
    My Mood
    Amazed
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Re: Static methods and variables

    It can change over time, but what I meant was it is the same for every class at any given time, right? Until one of the classes changes its value...

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Static methods and variables

    Quote Originally Posted by beer-in-box View Post
    It can change over time, but what I meant was it is the same for every class at any given time, right? Until one of the classes changes its value...
    yes, for any number of instances of that class, they all share the same static variable

Similar Threads

  1. Difference with static and non static variables
    By SkynetSystems in forum Java Theory & Questions
    Replies: 7
    Last Post: January 20th, 2013, 06:31 PM
  2. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  3. private static variables
    By tcstcs in forum Java Theory & Questions
    Replies: 4
    Last Post: June 28th, 2012, 01:00 AM
  4. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 08:13 AM
  5. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 06:10 PM