my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Using this for static field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Using this for static field" Watch "Using this for static field" New topic
Author

Using this for static field

geeta vemula
Ranch Hand

Joined: Jul 18, 2008
Posts: 208
source : http://www.examulator.com/phezam/showquestion.php

public class Honley{
public static int i =99;
public static void main(String argv[]){
Honley h = new Honley();
h.wine();
}
public void wine(){
i = 10;
int i = 20;
System.out.println(this.i);

}
}

Answer is : Compilation and output of 10

How come static int can be accessed with the help of this?
And is it possible to hide the variable in the same method?

Thanks,
Geeta Vemula.
xiao fei huang
Greenhorn

Joined: Nov 28, 2008
Posts: 7
As your definiation in method wine(), variable int i is a local varialbe. In a method, local variable's priority is higher than instance variable(static int i), so method int i override static int i. So no matter where your code try to do somthing to variable i after you declear local variable, and your action is on the local i but not static i.

You asked how to do something to static i but not local i with method wine(). It's simple. Remember static is a conception just belongs to CLASS. What you need to do is explicity tell JVM, I want to use "Honley.i", then JVM will understand what you mean is do something to static int i. Of course you could use "this.i" instead of "Honley.i" with the same meaning. Although static variable doesn't have this reference, but JVM still under stand what do you want to.

By the way, you could even make your example code more funny by changing the code order in method like this:



Notice that "i=10" after "int i = 20". At this time, "i=10" change the value of local int i but not static int because you have already decleared local int. But this.i(static int) still remeains 99. And the output will be 99.


I consider myselft as computer scientist.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Geeta

I think this is what is happening.

(1) How come static int can be accessed with the help of this? -- A static variable can be accessed via an instance or classname or even a null reference. So, the "this" here does not matter. Then, how come "this" correctly accesses the INSTANCE variable in your code?? Its because "this" always refers to the currently executing instance. And, in this case, it accesses the inherited static instance variable.

(2) And is it possible to hide the variable in the same method? -- You are defining a variable i that is local to wine(). So, it is not hiding because the static i is inherited but the i in wine() is totally a different variable, that too, a local variable. If you want to access the value of the local i, try this code: System.out.println(i); in place of your System.out.println(this.i);. It will display 20

Hope I am making sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using this for static field