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 4 of 4

Thread: What are the rules in using Static Method and Static Variable? HELP!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation What are the rules in using Static Method and Static Variable? HELP!

    Please teach me the rules in STATIC METHOD and STATIC VARIABLE.
    You can count the rules in NON-STATIC METHOD also, only if you want to post. But I need the rules in declaring STATIC METHOD AND STATIC VARIABLE. Thank you.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 168 Times in 159 Posts

    Default Re: What are the rules in using Static Method and Static Variable? HELP!

    Exactly what rules are you hoping people to tell you? As far as I know there are no strict rules like "thou shalt not ....."

    However, there are situations where you can use/call static or non-static methods. For a static method you can call it directly on the class or an object of that class. For non-static methods you must create an instance of that class first.
    Foo.staticMethod(); // OK
    Foo f = new Foo()
    f.staticMethod(); // OK
    Foo.nonStaticMethod(); // ERROR
    f.nonStaticMethod(); // OK
    Improving the world one idiot at a time!

  3. #3
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: What are the rules in using Static Method and Static Variable? HELP!

    A static variable is a variable that is shared across all instances of a class.

    A static method is a method that can be called on a class and usually does not require the class to be instantiated.

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: What are the rules in using Static Method and Static Variable? HELP!

    Static methods neither require an instance of the class nor can they implicitly access the data (or this, self, Me, etc.) of such an instance. A static method is distinguished in some programming languages with the static keyword placed somewhere in the method's signature.

    In statically typed languages such as Java, static methods are called "static" because they are resolved statically (i.e. at compile time) based on the class they are called on and not dynamically as in the case with instance methods which are resolved polymorphically based on the runtime type of the object. Therefore, static methods cannot be overridden.[9]

    -- Wikipedia

Similar Threads

  1. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum Java Theory & Questions
    Replies: 4
    Last Post: July 19th, 2013, 09:06 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. Replies: 4
    Last Post: November 14th, 2012, 11:09 PM