[Logo]
Forums Register Login
using static variable with object reference
public class A {
public static void main(String[] args) {
               B b = null;
System.out.println(b.number);
}

}


public abstract class B {
static String number= "123";
}

Please explain me how this code prints '123' correctly.
(1 like)
When posting code it's best to UseCodeTags so that it's easier to read. I've reposted your code using them below, doesn't it look much better?


Now, your question is "how this code prints '123' correctly?"

My question to you is "why do you think it shouldn't?"
(1 like)
Welcome to the Ranch

It is a quirk of the language that you can find a static member from a null. I think is shouldn't have been allowed, but the compiler links the static member to the reference. If you reference something as a Foo, then all static members of the Foo class are accessible from that reference, even if it points to null. You can even cast a null to any type because null itself doesn't have its own type:-Yes, it is pretty horrible code, but it will compile.
(1 like, 1 cow)
What seems to be happening "under the hood", looking at the bytecode (to confirm my suspicions) is that the compiler identifies the static value and simply replaces the reference with the class, so b.number becomes (essentially) B.number.
So it's not actually accessed via a null reference, it's just the compiler tidying up the developers bad code.
 

Dave Tolls wrote:. . . compiler tidying up the developers bad code.

I like it
 

Dave Tolls wrote:it's just the compiler tidying up the developers bad code.


Dave is clearly on compilers side
(3 likes)
Are you lot telling me you can't hear the compiler going "tut tut" and muttering under its breath when it encounters stuff like this?
;)
 

Dave Tolls wrote:Are you lot telling me you can't hear the compiler going "tut tut" and muttering under its breath when it encounters stuff like this?
;)


I will from now on. I have an image in my head of the compiler shaking its head disapprovingly. This has made me chuckle.
"static members can access instance members through an object reference. even if we can't create an instance of an abstract class  "
I think this the answer that I looking for. Please correct me if I am wrong.
 

manupriy logus wrote: "static members can access instance members through an object reference. even if we can't create an instance of an abstract class  "
I think this the answer that I looking for. Please correct me if I am wrong.


Something doesn't sound right to me.
 

manupriy logus wrote:. . . I think this the answer that I looking for. . . .

Please always tell us where such quotes come from, to avoid copyright problems.

I think that is correct after all, but it is the answer to a completely different question. It means that you can create a new object reference in a static method, like this:-If such code will compile, it is how you can gain access to an instance member. It is commonly seen in the main method. Lines 10‑11 can be joined together into a single line if you wish new FooDemo(...).run(...);Your original question was how can we gain access to a static member.
Something that's actually odd is that if you try to access a static member on an array element, the array and its bounds are checked:

There was actually a time where you could even use b[-1] without any exceptions, but this changed a few Java versions ago.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards


This thread has been viewed 294 times.

All times above are in ranch (not your local) time.
The current ranch time is
Jan 19, 2018 16:44:31.