• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Using this for static field

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 178
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic