wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Keyword this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Keyword this" Watch "Keyword this" New topic

Keyword this

J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46

Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
this.i refers to the class variable i in this case since this is a reference to an object of type Test.

The i declared inside the method is a local variable.
Maureen Augustus
Ranch Hand

Joined: Aug 18, 2006
Posts: 72
Hi J,

Because "int i = 20" is defined within a method, it is a local variable, not an instance variable.

The this keyword references instance variables, like the one you defined with the code "public static int i =99" and then assigned the value 10 within the method.

If you want to display the value "20", your output line would read "System.out.println(i);" to access the local variable instead of the instance variable.

"Sex and drugs and women being set on fire! I've never heard of such a Christmas!" - Christine Baranski in "The Ref"
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
Ah, so the 'this' keyword refers to the instance or class variable, but not to a local variable. Gotcha.

I agree. Here's the link: http://aspose.com/file-tools
subject: Keyword this
It's not a secret anymore!