Is this bad coding?

Is it bad to access Static members via instance reference?

IntelliJ highlight this kind of code.

Thanks.
-Hinke

0
Avatar
Permanently deleted user

Never mind.

I know what they are talking about now.

-Hinke

0
Avatar
Permanently deleted user

I think so. In C#, this will cause a compiling error.

0
Avatar
Permanently deleted user

Henrik Engert wrote:

Never mind.

I know what they are talking about now.


Well, i do not. :)
And i always wondered why this is considered "bad".
Could you please explain it to me?

ciao,
Stephan.

0
Avatar
Permanently deleted user

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"...

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

0
Avatar
Permanently deleted user

Greg Lucas wrote:

...
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? :)


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

0
Avatar
Permanently deleted user

WOW

I always surprise by seeing these kind of things. "Always Something to learn"

Thanks for the explanation.

-Anki.N

0
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

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

0
Avatar
Permanently deleted user

Why can't Java semantics be changed to not access the static members with instance reference, means forcing static members access only using class.


Because Java wants us to love IntelliJ IDEA for telling us that we've
called static method in class instances.

Franklin.

0
Avatar
Permanently deleted user

Anki wrote:

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() ;)


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

0
Avatar
Permanently deleted user

:) :)

0
Avatar
Permanently deleted user

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".

0

请先登录再写评论。