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.
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.
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.
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!
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.
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