views:

320

answers:

4

Can anyone explain to me why java allows you to access static methods and members from an instance? A bad example, if I have an object called RedShape and it has a static method called getColor() which returns "red", why does java allow you to call the static method from an instance of RedShape? To me this seems to violate some of the core concepts of OO language design. At the least, it should come with a compiler warning.

Thanks in advance.

Edit:

In particular, I'm asking about when you have something like

RedShape test = new RedShape();
test.getColor();

where getColor is a static method of the RedShape class. This doesn't make any sense that it's allowed and doesn't give a compiler warning on the command line via javac. I see that it's "strongly discouraged", but was curious if there was a technical or reasonable reason behind why it's allowed outside of "because C++ allows it."

+4  A: 

I don't see anything wrong with calling a static method from an instance. What's wrong with that? In particular, quite often there are methods which are useful within the logic of a class, but which don't actually need to manipulate the instance itself.

I do object to calling a static method via an instance reference. Classic example:

Thread thread = new Thread(...);
thread.sleep(5000); // Doesn't do what it looks like

This comes with a compiler warning in some IDEs - certainly in Eclipse, assuming you turn it on. (Java / Compiler / Errors and Warnings / Code Style / Non-static access to static member.) Personally I consider that a mistake in the design of Java. (It's one of the mistakes that C# managed to avoid copying.)

Jon Skeet
I think Brian is referring strictly to the latter abuse.
erickson
Tom Hawtin - tackline
Er, http://tinyurl.com/laojcp
Tom Hawtin - tackline
@Tom Hawtin: Exactly. The code doesn't do what it looks like it will do.
Jon Skeet
@erickson: You are correct. I was referring to the ability to reference a static method from an instance of the class containing the static method. I've revised my question to clarify.
Brian Hasden
A: 

The access to static methods allows you to share values between instances of the same class or even get the values without needed to create a class instance.

There are cases where it's convenient and is no OO language violation.

Carlos Tasada
+1  A: 

There's really no reason why you can actually do this.

My only guess was that it would allow you to override static methods, but you can't.

If you try the following scenario:

Banana has a static method called 'test' (this prints 'banana') Apple extends Banana and "overrides" the static method called 'test' (this prints 'apple')

and you do something like this:

public static void main(String[] args) {
    Apple apple = new Apple();
    Banana banana = new Banana();
    Banana base = new Apple();

    apple.test();
    banana.test();
    base.test();
}

The resulting output is:

apple
banana
banana

So effectively, it's pretty useless.

Malaxeur
A: 

I bet it's because the original designers were porting capability from C++, and by the time 20/20 hindsight hit, it was a backwards compatibility issue.

That, or because when you call a method within a class, even though you don't have to prefix everything with this. the compiler inserts it (or equivalent) including for static methods. If static methods couldn't be called from instances, then tacking this. on the front might be a problem (or would force coders to tack class name on the front of static methods whenever they wanted to use them within an actual instance).

Anyway, the answers are speculative unless we get one of the early language developers to answer.

Carl