Static method in Java can be accessed using object instance [duplicate]
In Java static methods are created to access it without any object instance. It makes some sense to me. But recently I came across one strange thing that, static method in Java can also be accessed through its object instance. This looks pretty wierd to me. Does anyone of you know why this feature is provided by Java? Whats the significance of allowing static methods getting accessed with as well as without instance?
A benefit of this is that it allows you to take an instance method and turn it into a static method without having to modify any existing code (other than the class) and thus allowing for backwards compatibility. I've found this useful as many times I've come across utility methods that can be made static - I can just add the static
modifier and continue on my way.
Semantically identical. The compiler is smart enough to know what you mean (that is access the static method through the class). IDEs will give you a warning telling you it's bad manners :)
Look at this question for more details. As they say it can be misleading and that's why IDEs will give you a warning.
It's allowed by spec, but it's not recomended. Further more IDE's like Eclipse mark access to a static method on an object instance with an warning.
while it's bad, there is no compelling reason to forbid it either.
o.f();
so we need to find a method named f
in the scope of o
. One could argue that a static f
is of course also in the scope of o
, even though f
is actually defined for a larger scope (o
's class)
Another nice (albeit somewhat hack-y) feature that it gives you is the ability to essentially pass around class references in the form of objects. So say, for example, that you have the following:
public abstract class Animal {
public String name() { return "animal"; }
}
public class Dog extends Animal {
public String name() { return "dog"; }
}
If you then run the following:
Animal a = new Dog();
System.out.println(a.name());
...you will get:
dog
What this means in practice is that calling static methods through instantiated objects provides Java with a form of first-class functions. You define the function "type" using an abstract base class, and then instantiate any function of the proper type by overriding the static method in a child class. You then use the objects as though they were simply containers for the static method in question.
To make this more concrete, imagine that we wanted to have a function which performs a certain string encoding on an array of strings, and we want it to take as an argument a function which performs encoding on a single string. We want the argument to vary based on what encoding is desired. Without first-class functions, this is impossible to do directly. However, calling static methods on objects provides us with a workaround:
public abstract class EncodingType {
public String encode(String s) { return s; }
}
public class Base64Encoding extends EncodingType {
public String encode(String s) { base64Encode(s); } // Assume "base64Encode" is defined
}
public class Rot13Encoding extends EncodingType {
public String encode(String s) { rot13Encode(s); } // Assume "rot13Encode" is defined
}
public class Encoder {
public String[] encodeArray(String[] s, EncodingType enc) {
for (int i = 0; i < s.length; i++) {
s[i] = enc.encode(s[i]);
}
return s;
}
}
You would call it like this:
Encoder e = new Encoder();
String[] strs = { ... };
strs = e.encodeArray(strs, new Rot13Encoding());
精彩评论