On Wed, 01 Sep 2004 09:06:56 +0200, Stephan Jätzold <stephan@jaetzold.de> wrote:
(Calling static methods on an instance rather than the class)
And i always wondered why this is considered "bad".
This is not an error, it can just be confusing. A static method does not have access to instance data, so calling it on an instance rather than the class misrepresents what is actually happening. Any misinformation in code is considered "bad"...
The second call will work just fine, but that is certainly not intuitive from the code. The JVM ignores the obj instance completely, so why don't you? :)
The second call will work just fine, but that is certainly not intuitive from the code. The JVM ignores the obj instance completely, so why don't you? :)
I didn't know that would work! I guess that I should read the Language Spec.
That's the most convincing argument that I've seen for avoiding accessing Static members via instance references.
Many programmers (probably including the guy who wrote it)will look at this and assume that the distant thread is being put to sleep for a second. However, Thread.sleep() is actually a static method, and it puts the current thread to sleep, ignoring it's presumed target completely.
You can spend days tracking that sort of thing down.
Why can't Java semantics be changed to not access the static members with instance reference, means forcing static members access only using class.
For the example obj = null; obj.staticMethod(); //
this really makes no sense. Whatever happens internally (may be compiler replaces this statement as OBJCLASS.staticMethod()), this logically looks like null.staticMethod() ;)
I found some interesting things with decompiled version of this kind of code.
Original code
decompiled code
Some mysterious _tmp variables are there. These _tmp variables are appearing only if we access static with instance reference.
Why can't Java semantics be changed to not access the static members with instance reference, means forcing static members access only using class.
For the example obj = null; obj.staticMethod(); //
this really makes no sense. Whatever happens internally (may be compiler replaces this statement as OBJCLASS.staticMethod()), this logically looks like null.staticMethod() ;)
Never mind.
I know what they are talking about now.
-Hinke
I think so. In C#, this will cause a compiling error.
Henrik Engert wrote:
Well, i do not. :)
And i always wondered why this is considered "bad".
Could you please explain it to me?
ciao,
Stephan.
On Wed, 01 Sep 2004 09:06:56 +0200, Stephan Jätzold <stephan@jaetzold.de>
wrote:
(Calling static methods on an instance rather than the class)
This is not an error, it can just be confusing. A static method does not
have access to instance data, so calling it on an instance rather than the
class misrepresents what is actually happening. Any misinformation in code
is considered "bad"...
Also, consider the following:
MyClass obj = new MyClass();
obj.someStaticMethod(); //1
obj = null;
obj.someStaticMehod(); //2
The second call will work just fine, but that is certainly not intuitive
from the code. The JVM ignores the obj instance completely, so why don't
you? :)
--
Greg
Greg Lucas wrote:
I didn't know that would work! I guess that I should read the Language
Spec.
That's the most convincing argument that I've seen for avoiding
accessing Static members via instance references.
Tim
WOW
I always surprise by seeing these kind of things. "Always Something to learn"
Thanks for the explanation.
-Anki.N
The canonical bad example is
Many programmers (probably including the guy who wrote it)will look at this and assume that the distant thread is being put to sleep for a second. However, Thread.sleep() is actually a static method, and it puts the current thread to sleep, ignoring it's presumed target completely.
You can spend days tracking that sort of thing down.
--Dave Griffith
Interesting discussion and findings.
This raises me a question
Why can't Java semantics be changed to not access the static members with instance reference, means forcing static members access only using class.
For the example
obj = null;
obj.staticMethod(); //
this really makes no sense. Whatever happens internally (may be compiler replaces this statement as OBJCLASS.staticMethod()),
this logically looks like null.staticMethod() ;)
I found some interesting things with decompiled version of this kind of code.
Original code
decompiled code
Some mysterious _tmp variables are there. These _tmp variables are appearing only if we access static with instance reference.
See I found an Easter Egg (Not 1 total 4) ;)
Any hints?
Thanks in advance
Because Java wants us to love IntelliJ IDEA for telling us that we've
called static method in class instances.
Franklin.
Anki wrote:
In fact this is exactly what happens as is specified in the JLS:
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#45677
That's why I said earlier "I guess that I should read the Language
Spec." Perhaps we all should.
Tim
:) :)
And more - static methods aren't overriden:
class Base {
void method() {System.out.println("Base"}
}
class Sub extends Base {
void method() {System.out.println("Sub"}
}
Base sub = new Sub();
sub.method();
-> usual polymorphic overriding should result in "Sub".
-> actual output: "Base".