Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing static variable via null reference

 
amit ang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class Test {
public static int intTmp = 10;
public Test() {
}
public static void main(String[] args) {
Test test1 = new Test();
test1=null;
System.out.println("Here it is : "+test1.intTmp);
}
output :
Here it is : 10
how is this possible any good explaination on this ?
cheers,
Amit
[ EJFH -- added sensible subject line ]
[ April 28, 2004: Message edited by: Ernest Friedman-Hill ]
 
Himanshu Pathak
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just remove the static keyword and try to execute your code.
it will throw a NullPointerException
also when i compiled your code it gives a warning which is quite understood.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and a beginner topic too...
... and a bad subject ...
[ April 28, 2004: Message edited by: Stefan Wagner ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stefan Wagner:
... and a beginner topic too...
... and a bad subject ...

Indeed. Changed subject, moved to JiG (Beginner).
 
Jeffrey Hunter
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not need an actual instantiation of the class to call the static variable.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Amit, a static field is associated with the class, not with an instance of the class. Therefore, Java uses the data type of the variable to resolve "intTmp". It doesn't matter that the reference is null, because it doesn't need the reference to get the value of intTmp.
 
amit ang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,
Ya, i know that static is identified by class. But, when you access it through reference
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
your reference doesn't point to anything...
NEVER access a static method/member through a reference, it's bad practice (even though not actually prohibited).
A static member doesn't relate to any instance, so accessing it as if it did is a big nono.
 
amit ang
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys,
Got it. It's all to do with the associated TYPE rather then instance for the static declarations.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic