Posted on 2006-04-30
Last Modified: 2010-03-31
Can anyone tell me what exactly Static means and when it should be used.  I kinda know when to use it but I am not sure I quite get the concept of it.

Question by:yattias
    LVL 24

    Expert Comment

    static means: global (always present) for a variable.
                        accessing static variables for a method.
                        to be executed at startup for a block {}

    Example:     static int count = 0;    // to count objects of this kind, 0 == no objects, still a variable,
                      static void counter() { ++ count; }  // to access this variable.


    Author Comment

    I am not so sure I fully understand your definition of static.  Could you be more specific?
    LVL 23

    Accepted Solution

    Generally Static varaibles are used to share some thing b/w the Objects.

    In simple terms Static variables doesn't belong to any Object in particular they belong to the Class.

    They are Very Useful in situations when you want to count the Number of Objects of the Class are created for example.

    In similar fashion Static method also Belong to the Class So only when you want to use them you just need to say Classname.functionName.

    check out
    LVL 30

    Expert Comment

    Basically its a member of the class for instantiating or using which you don't need to create objects of the class.

    Explained here:
    LVL 24

    Assisted Solution

    And static variables and methods are bundled in classes just to make them globally
    usable without any object relationship.
    For instance:
    the class Math bundles all static calculation methods, like abs() and sqrt().
    It would be weird to create an object, just to compute a square root.

    LVL 30

    Expert Comment

    Yes, you can use them for utilities (where you would expect the same result from a method and you don't need any instance data-members), and static is also made use of in the singleton design pattern.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    Title # Comments Views Activity
    how to extract window origin in spring 2 55
    countClumps  challenge 10 66
    countX 22 48
    java  and programming certification ? 4 25
    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now