• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Accessing static variable via null reference

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and a beginner topic too...
... and a bad subject ...
[ April 28, 2004: Message edited by: Stefan Wagner ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 305
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not need an actual instantiation of the class to call the static variable.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Max,
Ya, i know that static is identified by class. But, when you access it through reference
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic