Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!post01.iad.highwinds-media.com!newsfe13.iad.POSTED!8ad76e89!not-for-mail From: Arved Sandstrom User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.28) Gecko/20120313 Lightning/1.0b2 Thunderbird/3.1.20 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: this References: <6cab49af-72f5-4bb4-87f8-7eb2e6330b05@t23g2000yqd.googlegroups.com> In-Reply-To: <6cab49af-72f5-4bb4-87f8-7eb2e6330b05@t23g2000yqd.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Lines: 77 Message-ID: <_PSmr.61335$T5.32653@newsfe13.iad> X-Complaints-To: abuse@newsgroups-download.com NNTP-Posting-Date: Sat, 28 Apr 2012 14:06:50 UTC Organization: Public Usenet Newsgroup Access Date: Sat, 28 Apr 2012 11:06:49 -0300 X-Received-Bytes: 3625 Xref: csiph.com comp.lang.java.programmer:13964 On 12-04-28 09:33 AM, mojde wrote: > hi all , > I can't understand the usages of "this" , could you plz help me ? > > thank you . . . One way of understanding "this" is as part of a larger picture, which is understanding what everything is doing at runtime, i.e. when your code is executing. Once you've fired up a Java application, and execution of code starts with a proper main() method in a designated main class [1], objects with various lifetimes start to be created. Code in _instance_ methods (as opposed to static/class methods) runs in the context of a single object. It's this single object that you can refer to as "this". Understand also that often, by well-defined rules, you can omit the explicit "this". A "this" reference (implicit or explicit) is how you affect the state of the object that your instance methods are executing in. See above: when an instance method executes it is in the context of one object. Getters and setters, for example, wouldn't be very useful unless they could access the state of the particular object. Getters and setters are simple instance methods themselves. Without the "this" reference you effectively lose encapsulation. You'd have to make your member fields public, and there really wouldn't be instance methods anymore as you'd have to pass object references around and that would be useless. As an example, consider a rudimentary class Vehicle as public class Vehicle { private Double speed; public Double getSpeed() { return speed; } public void setSpeed(Double speed) { this.speed = speed; } } Those 2 accessor methods are instance methods - whenever that code executes they have a context of a single, instantiated Vehicle object. So for example, Vehicle vehicle1 = new Vehicle(); vehicle1.setSpeed(15.5); When you call setSpeed() on "vehicle1", the 'this' reference in the method body is how the code can access the member field "speed", by 'this.speed', and actually set the value. The getter _implicitly_ uses 'this'. If you didn't have 'this', things would degrade to public class Vehicle { public Double speed; } and Vehicle vehicle1 = new Vehicle(); vehicle1.speed = 15.5; There is another usage of 'this' for invoking another constructor from a constructor of the same class, but I won't cover that right now. Although in a conceptual sense it's yet another mechanism for accessing something belonging to the current object. AHS 1. A codebase can have any number of "main" classes. As many of those that the codebase has, is how many individual Java applications the codebase supports. -- A fly was very close to being called a "land," cause that's what they do half the time. -- Mitch Hedberg