Static method

Why to call static methods using class name though we can call static method by using object also....?
[109 byte] By [prady_joa] at [2007-11-26 18:55:06]
# 1
It's a convention. Static methods are class methods not instance methods, that's why you should use the class.
PhHeina at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 2
Sometimes there is no appropriate object but you still want to invoke that static method.
BIJ001a at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 3
It makes it a lot easier to read the code, too. Making it easier for others to read your code is very helpful in a group environment.If you see the class name, you know it's a static method that's being called.
paulcwa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 4
plz explain in detail....!
prady_joa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 5
plz read the tutorial.....
mkoryaka at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 6
> plz explain in detail....!If you don't understand the explanations given, just prefix with the class name for the time being. It's considered good practice.
mkoryaka at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 7
> It's> considered good practice.On the other hand it's not unlikely that the static imports will change this notion somewhat. Static imports allow you to omit prefixing with the class name altogether.
mkoryaka at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 8

> just

> prefix with the class name for the time being. It's

> considered good practice.

In fact, if you write objX.staticMethod() instead of ClassX.staticMethod some

compilers will warn you that it's not good practice to hitch a ride on the object

like that!

DrLaszloJamfa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 9

>Why to call static methods using class name though we can call static method by using >object also....?

that is the whole point. teh static methods can be instantiated without using objects. For example. Static allows us to call main() without having to instantiate any object. it is called before any object is created. hence for example it is declared always as

public static void main()

ninjavaa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 10
static imports are a huge mistake and anyone using them should be shot, lest I end up having to work on their code.~TimIf anyone can give me a case where they are acceptable, I'd like to hear it, as it is I think they should be removed from the language spec.
SomeoneElsea at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 11

> static imports are a huge mistake and anyone using

> them should be shot, lest I end up having to work on

> their code.

>

> ~Tim

>

> If anyone can give me a case where they are

> acceptable, I'd like to hear it, as it is I think

> they should be removed from the language spec.

Well, how about this case: you're using java.io.StreamTokenizer. When doing so, you regularly have to compare tokens type codes with constant values defined in StreamTokenizer. So with static imports, you can have code like:

if (type == TT_NUMBER) {

instead of:if (type == StreamTokenizer.TT_NUMBER) {

which gets a little hairy when you have a dozen of those.

paulcwa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 12

Consider what Sun wrote on the page for this new feature (http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html):

<quoteblock>

So when should you use static import? Very sparingly! Only use it when you'd otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

</quoteblock>

Compare these two imports:

import static java.lang.Math.PI;

import com.acme.widget.Something;

If I see class Something used in the code, I'll look for it in the current

package, and not finding it, notice it listed in the imports.

If I see PI used in the code, I'll look for it as a local variable, then a field,

and not finding it, notice it listed in the imports.

Aren't those cases similar? If you countenance the first, why not the second?

DrLaszloJamfa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 13

OK, I stand corrected. But you know that there are those people out there who like to call themselves programmers who will do stuff like

import static java.lang.Math.*;

import static javax.swing.SwingConstants.*;

just to use one or 2 constants (or methods). These are the people I fear working with.

~Tim

SomeoneElsea at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 14

As the name itself says, static members are meant for static information i.e, something which is common to all the instances of a class. Let me give a simple example:

Say you have a class by name Cat which contains a static method by name getCatCount() which keeps track of the no. of Cat instances created so far, then it would not make any sense at all to call this method on a particular instance of the Cat class because it is not specific to an instance of the class.

Kiran_Joisa at 2007-7-9 20:32:54 > top of Java-index,Java Essentials,New To Java...
# 15
> then it> would not make any sense at all to call this method> on a particular instance of the Cat class because it> is not specific to an instance of the class.If it makes no sense, why is it allowed?
DrLaszloJamfa at 2007-7-9 20:33:00 > top of Java-index,Java Essentials,New To Java...
# 16

> OK, I stand corrected. But you know that there are

> those people out there who like to call themselves

> programmers who will do stuff like

>

> import static java.lang.Math.*;

> import static javax.swing.SwingConstants.*;

That's why projects need coding standards.

DrLaszloJamfa at 2007-7-9 20:33:00 > top of Java-index,Java Essentials,New To Java...
# 17
because you can call a static method even there is no Objects .by classname then method name you can access the static method even if the class didn't instantiated once
eaajea at 2007-7-9 20:33:00 > top of Java-index,Java Essentials,New To Java...