• Post Reply Bookmark Topic Watch Topic
  • New Topic

using static variable with object reference  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 4141
247
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?"
 
Marshal
Posts: 56929
174
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Foreman
Posts: 3139
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 56929
174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

I like it
 
Sheriff
Posts: 5102
357
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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


Dave is clearly on compilers side
 
Dave Tolls
Ranch Foreman
Posts: 3139
37
  • X Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
;)
 
Tim Cooke
Marshal
Posts: 4141
247
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
manupriy logus
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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.
 
Liutauras Vilda
Sheriff
Posts: 5102
357
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Campbell Ritchie
Marshal
Posts: 56929
174
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Sheriff
Posts: 21187
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!