problem in concept of static

Static members are the belonging of class and not of object, but static members are still accessed through the object refferences..how and why it is possible?
[172 byte] By [sha545504a] at [2007-10-3 3:00:49]
# 1

> Static members are the belonging of class and not of

> object,

Correct.

> but static members are still accessed through

> the object refferences..

Wrong.

> how and why it is possible?

It's possible simply because every reference has a type and thus a class associated. You are not supposed to call static members using a reference. It's bad coding style.

CeciNEstPasUnProgrammeura at 2007-7-14 20:50:27 > top of Java-index,Java Essentials,New To Java...
# 2
according to what i have studied>object only contains instance membersand methods and not the static members. then why objectis able to call those static members?Message was edited by: sha545504
sha545504a at 2007-7-14 20:50:27 > top of Java-index,Java Essentials,New To Java...
# 3

> according to what i have studied

> >object only contains instance members

>and methods and not the static members.

Correct.

> n why objectis able to call those static members?

Object calls nothing. It's a courtesy of the compiler to link that method call to the class.

CeciNEstPasUnProgrammeura at 2007-7-14 20:50:27 > top of Java-index,Java Essentials,New To Java...
# 4

> according to what i have studied

> >object only contains instance members

>and methods and not the static members.

> n why objectis able to call those static members?

>

Interesting question. It's a language "feature". I guess they did this, so you don't break compatibilty, if you make methods of some class static.

But you are right: there is no reason why you can automatically access static fields and attributes from within a class. This was enabled explicitely, but for what reason: I don't know.

Mongera at 2007-7-14 20:50:27 > top of Java-index,Java Essentials,New To Java...
# 5

> > according to what i have studied

> > >object only contains instance members

> >and methods and not the static members.

> > n why objectis able to call those static members?

> >

>

> Interesting question. It's a language "feature". I

> guess they did this, so you don't break compatibilty,

> if you make methods of some class static.

They didn't do this because the object doesn't do anything:

class Test {

private static int x = 12;

public static void main(String[] sgra) {

Test t = null;

System.out.println(t.x);

}

}

It does not throw a NullPointerException, so the reference isn't even touched.

> But you are right: there is no reason why you can

> automatically access static fields and attributes

> from within a class.

Instance you mean?

> This was enabled explicitely,

> but for what reason: I don't know.

Convenience, I'd say. So you can write

if (arg == STATIC_CONSTANT)

even in a non-static context without having to prefix it with the class.

CeciNEstPasUnProgrammeura at 2007-7-14 20:50:27 > top of Java-index,Java Essentials,New To Java...