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