This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 4463
284
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: 60083
188
  • 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.
 
Master Rancher
Posts: 3627
39
  • 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: 60083
188
  • 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: 6008
415
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
Master Rancher
Posts: 3627
39
  • 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: 4463
284
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: 6008
415
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: 60083
188
  • 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: 21452
95
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!