• 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

can we able to refer a static variable using an object

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I came across this following code
public class Test {
static int total = 10;
public static void main (String args []) {
Test t1=new Test();
System.out.println(t1.total);
}
public Test () {

int temp = this.total;
if (temp > 5) {
System.out.println(temp);
}
}
}

OUTPUT :
10
10


I studied that static varaiables must be called using the CLASS Name but how this program works ??
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is considered better style to use the class name when referencing static variables, but it is not illegal to use instance references.

Note however that the instance isn't actually dereferenced. The following code will not throw a NullPointerException
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler may produce a *warning* but not an error if a static member of a class is being accessed by its instance though its suggested to be used with the ClassName - thats what the purpose is.


Note however that the instance isn't actually dereferenced. The following code will not throw a NullPointerException

code:
--------------------------------------------------------------------------------

Test t1= null;System.out.println(t1.total);

--------------------------------------------------------------------------------




Thats a good point Joanne Neal
 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But Raghavan/Jonnan

what do you mean by the instance not getting derefenced in the static context
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It means the the compile-time type of the instance is used to bind the method invocation to a method at compile time, it is not bound at run time polymorphically . The instance itself is never used to bind the method invocation to a method, thats why a NullPointerException isn't thrown when a static method is called using a null reference. Static methods aren't polymorphic which is why calling a static method using an instance reference is confusing to read and considered bad style.



output:


[ June 09, 2007: Message edited by: Garrett Rowe ]
[ June 09, 2007: Message edited by: Garrett Rowe ]
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Generally when you invoke a member of class (variable or the method) through its instance, the instance variable (reference variable) is actually looked upon to get to know *the actual object* to invoke the member.

Lets say,



If you run the above program, what would be the output? It would be as follows as you expected!



Can you just think for a while how the output is actually obtained?

In both line 1 & 2, the actual references (objects) s1 & s2 are looked for the inner details (bit structure on how to reach the individual objects they are pointing to) so as to get the correct object! . Thats why you get the correct output as s1's a is 1 and s2's a is 5. Aint I?

this is what you can call the "object is dereferenced" - means the object is being checked upon!

What if you have an object as null and try to access the "a" variable on it? - as follows:



Here at runtime, the reference variable s3 is being looked upon for reaching the actual object so as to get its variable a and obtain its value! But actually the reference varaible "s3" is pointing nowhere. How can it obtain a value on a NULL Reference? Thats when and why, you will get a NullPointerException!.

But in case of the original post, when you access the static member of the class even with the null object reference, the object is NOT dereferenced, because the static members are not aware and don't care about any instance of the class as they are tied to the Class and NOT to any instances!

Moreover, by the time, the static members are created, there would have been NO instances created!! Thats why you *don't get any exception* in that case!

Is that clear?
reply
    Bookmark Topic Watch Topic
  • New Topic