Upload
joetuc
View
213
Download
0
Embed Size (px)
Citation preview
The Java Tutorial: A Sho…
Sharon Zakhour, …
$32.39
The Java EE 6 Tutorial: Basic …
Eric Jendrock, Ia…
$31.49
Head First Java, 2nd Edition
Kathy Sierra, Bert…
$26.31
Head First HTML with CSS & XHT…
Eric T Freeman, E…
$23.41
Java Programming for a Beginner,…
DVDxDVD
The Java(TM) Tutorial Contin…
Mary Campione, …
$44.40
Programming Java Introduct…
Programming in J…
$49.99
Get Widget Privacy
Search Amazon.…
∑ Home ◇ Math ◇ Computing ◇ Arts ◇ Words ◇ Literature ◇ Music ◆ ◇ Search XahLee.org here Search
Beauty & FashionCosmeticsCosmetic SurgeryDesigner FootwearPerfume
ComputersComputer GamesDomain HostingDomain RegistrationFree Internet
ElectronicsDVDsDigital CamerasLaptopsMobile Phones
EntertainmentBooksConcert TicketsGamesFantasy Football
Chitika | Select
If you spend more than 60 minutes on the site, please pay USD $6. Thank you
Java Tutorial: “this” Keyword
Xah Lee, 2005-02
In java there's “this” keyword. It can be used inside methods (and constructors).
It returns the reference to the current object.
For example, if you have a class named CL, and it has method named “me”, then “this” in it would be a reference to a instance of the CL (that is: a CL object).
Example 1
class CL { int x = 1; CL me () {return this;} } public class thiss { public static void main(String[] args) { CL cl = new CL(); System.out.println( cl.x ); System.out.println( cl.me().x ); // same as above System.out.println( cl.me().me().x ); // same as above } }
In the above example, the method “me” returns “this”. So, 「cl.me
()」 is equivalent to the object cl itself. Therefore, 「cl.x」 and 「cl.me().x」 and 「cl.me().me().x」 are all the same.
Java Tutorial
Page 1 of 4Java Tutorial: “this” Keyword
5/27/2011http://xahlee.org/java-a-day/this.html
Advertise Here For Profit
Example 2
Another example:
class OneNumber { int n; void setValue (int n) {this.n=n;}; } public class thatt { public static void main(String[] args) { OneNumber x = new OneNumber(); x.setValue(3); System.out.println( x.n ); } }
In the above example, the method setValue tries to set the value of its argument to the class variable n. Because the name n is already used in the parameter name, so 「n=n」 is absurd. The workaround is to use the “this” keyword to refer to the object. Thus we have 「this.n=n」.
Example 3
Another practical example of using “this” is when you need to pass your current object to another method. Example:
class B { int n; void setMe (int m) { C h = new C(); h.setValue(this, m); }; } class C { void setValue (B obj, int h) {obj.n=h;}; } public class A { public static void main(String[] args) { B x = new B(); x.setMe(3); System.out.println( x.n ); } }
In the above example, B has a member variable n. It has a method setMe. This method calls another class method and passing itself as a object.
There is also a “super” keyword used to refer to the parent class. See “super” keyword.
Java Tutorial
Page 2 of 4Java Tutorial: “this” Keyword
5/27/2011http://xahlee.org/java-a-day/this.html
8 people liked this.
The Java EE 6 Tutorial
Eric Jendrock, Ian...
New $31.49Best $25.95
Head First Java, 2nd Edition
Kathy Sierra, Bert...
New $26.31Best $19.87
The Java Tu
Sharon Zak
New $32.39Best $20.99
You like:
What are OOP's Jargons and Complexities?•
Add New Comment
Showing 4 comments
Sort by Popular now Subscribe by email Subscribe by RSS
gudd>>>
1 person liked this. Like
nice
1 person liked this. Like
Excellent _abhi_
1 person liked this. Like
Java Tutorial
Type your comment here.
Post as … Image
rohit 2 months ago
Reply
Suma 4 months ago
Reply
Abhishek Pvu 5 months ago
Reply
Bhathiya 2 months ago
Page 3 of 4Java Tutorial: “this” Keyword
5/27/2011http://xahlee.org/java-a-day/this.html
Nice one understood the this keyword
Like
blog comments powered by DISQUS
∑ Home ◇ Terms of Use ◇ About ◇ Advertise ◇ Subscribe© 2011 Xah Lee.
Java TutorialReply
Page 4 of 4Java Tutorial: “this” Keyword
5/27/2011http://xahlee.org/java-a-day/this.html