Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

33
  • 13
    @Bemrose: But that's my point: Why shouldn't I be allowed to do that? Maybe my intuitive concept of what a "static" should do is different from yours, but basically I think of a static as a method that CAN be static because it doesn't use any instance data, and which SHOULD be static because you may want to call it independently of an instance. A static is clearly tied to a class: I expect Integer.valueOf to be tied to Integers and Double.valueOf to be tied to Doubles. Commented Feb 8, 2010 at 20:12
  • 10
    @ewernli & Bemrose: Yes yes, that's how it IS. I'm not debating that. As the code in my example doesn't work, of course I wouldn't try to write it. My question is why it is that way. (I fear this is turning into one of those conversations where we're just not communicating. "Excuse me Mr Salesman, can I get one of these in red?" "No, it costs $5." "Yes, I know it costs $5, but can I get a red one?" "Sir, I just told you that it costs $5." "Okay, I know the price, but I was asking about the color." "I already told you the price!" etc.) Commented Feb 8, 2010 at 20:29
  • 7
    I think that ultimately this code is confusing. Consider if the instance is passed as a parameter. Then you are saying that the runtime instance should dictate which static method is called. That basically makes a whole separate hierarchy parallel to the existing instance one. Now what if a subclass defines the same method signature as non-static? I think that the rules would make things quite complicated. It is precisely these kinds of language complications that Java tries to avoid. Commented Feb 8, 2010 at 22:24
  • 7
    @Yishai: RE "Runtime instance dictate which static method is called": Exactly. I don't see why you can't do anything with a static that you can do with a virtual. "Separate hierarchy": I'd say, Make it part of the same hierarchy. Why aren't statics included in the same hierarchy? "Subsclass defines the same signature non-static": I presume that would be illegal, just like it's illegal to, say, have a subclass override a function with an identical signature but a different return type, or to not throw all the exceptions that the parent throws, or to have a narrower scope. Commented Feb 9, 2010 at 5:34
  • 31
    I think Jay has a point - it surprised me too when I discovered that statics could not be overridden. In part because if I have A with method someStatic() and B extends A, then B.someMethod() binds to the method in A. If I subsequently add someStatic() to B, the calling code still invokes A.someStatic() until I recompile the calling code. Also it surprised me that bInstance.someStatic() uses the declared type of bInstance, not the runtime type because it binds at compile not link, so A bInstance; ... bInstance.someStatic() invokes A.someStatic() if if B.someStatic() exists. Commented Feb 25, 2010 at 18:46