• Post Reply Bookmark Topic Watch Topic
  • New Topic

can we able to refer a static variable using an object  RSS feed

 
yogesh srinivasan
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • 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 ??
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • 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
 
Raghavan Muthu
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
But Raghavan/Jonnan

what do you mean by the instance not getting derefenced in the static context
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!