• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

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.
 
Sheriff
Posts: 4583
286
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: 62231
193
  • 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.
 
Rancher
Posts: 3797
40
  • 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: 62231
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

I like it
 
Marshal
Posts: 6363
437
BSD Linux Mac OS X VI Editor
  • 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
Rancher
Posts: 3797
40
  • 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
Sheriff
Posts: 4583
286
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
Marshal
Posts: 6363
437
BSD Linux Mac OS X VI Editor
  • 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: 62231
193
  • 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: 21555
100
Chrome Eclipse IDE Java Spring Ubuntu VI Editor 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.
 
"How many licks ..." - I think all of this dog's research starts with these words. Tasty tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!