aspose file tools
The moose likes Beginning Java and the fly likes can we able to refer a static variable using an object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "can we able to refer a static variable using an object" Watch "can we able to refer a static variable using an object" New topic
Author

can we able to refer a static variable using an object

yogesh srinivasan
Ranch Hand

Joined: Jun 08, 2007
Posts: 55
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

Joined: Aug 05, 2005
Posts: 3011
    
    9
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


Joanne
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

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


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
yogesh srinivasan
Ranch Hand

Joined: Jun 08, 2007
Posts: 55
But Raghavan/Jonnan

what do you mean by the instance not getting derefenced in the static context
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1295
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 ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3327

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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: can we able to refer a static variable using an object
 
Similar Threads
what does this mean here?
Using this to reference static variables
static??
Access to static variable
Diff b/w "this" & "toString"