> 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!
>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()
> 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.
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?
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
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.
> 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.