[3614] in java-interest

home help back first fref pref prev next nref lref last post

Re: What's the deal with the variable *this*?

daemon@ATHENA.MIT.EDU (Arshad Noor - Technical Mgr - Sun )
Fri Nov 17 13:23:11 1995

Date: Fri, 17 Nov 1995 10:06:18 -0500
From: anoor@hedley.East.Sun.COM (Arshad Noor - Technical Mgr - Sun Integration)
To: java-interest@java.Eng.Sun.COM, michael@w3media.com


 >From daemon@java Thu Nov 16 23:58 EST 1995
 >Mime-Version: 1.0
 >Date: Thu, 16 Nov 1995 17:26:38 -0700
 >To: java-interest@java.Eng.Sun.COM
 >From: michael@w3media.com (Michael Mehrle)
 >Subject: What's the deal with the variable *this*?
 >Precedence: bulk
 >X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
 >
 >Here's one question I originally e-mailed to John December (author of
 >"Presenting Java"). If anyone else could answer this question, it would be
 >_greatly_ appreciated!
 >
 >Congratulations to your book, it's a whole lot better than Java! (by Tim
 >Ritchey). I'm a bloody beginner and your book helped a lot to master the
 >"basicbasics" of Java. Hope that makes you feel better.;)
 >
 >I've got a question though: Here is a copy of your Segment class (please
 >check out my comments, they're explained below):
 >
 >
 >class Segment {
 >
 >   private String thePlace = "Unknown";
 >   private int segmentNumber;
 >
 >   Segment (int number) {
 >      segmentNumber = number; // *here's the bad boy, shouldn't it say:
 >                                  // this.segmentNumber = number; ?
 >   }
 >
	When a method refers to a variable inside the same class,
	the "this" variable is implicit when it is used.  The
	only times you need to use "this" is when you need to use
	it *explicitly* to prevent ambiguities.

	For example, if your above code had been written as:

		Class Segment {
			private int segmentNumber;
			Segment (int segmentNumber) {
				this.segmentNumber = segmentNumber;
			}

	the "this" is required to prevent confusing the compiler.


================================================================================
Arshad Noor							400 Atrium Drive
Technical Manager					       Somerset NJ 08873
Sun Integration						   Voice: (908) 302-3822
Arshad.Noor@East.Sun.Com				   Fax:   (908) 469-4098
================================================================================




 >   Segment(int number, String aPlace) {
 >      this(number);
 >      thePlace = aPlace;      // also here: this.thePlace = aPlace; ?
 >   }
 >
 >   public void header() {
 >      System.out.println("Segment #" + segmentNumber + " " + thePlace);
 >   }
 >
 >   public void action() { }
 >
 >   public void cut() {
 >      System.out.println("---------------");
 >   }
 >}
 >
 >Saw the remarks above? So this should apply to the subclass also then:
 >
 >class Dialogue extends Segment {
 >
 >   private Person personA;
 >   private Person personB;
 >
 >   Dialogue (int number) {
 >      super(number);
 >   }
 >
 >   Dialogue(int number, String aPlace) {
 >      super(number, aPlace);
 >   }
 >
 >   Dialogue (int number, String aPlace,
 >             String actor1Name, String actor2Name) {
 >      this(number, aPlace);
 >      personA = new Person(actor1Name); // this.personA = new
 >Person(actor1Name);?
 >      personB = new Person(actor2Name); // this.personA = new
 >Person(actor1Name);?
 >   }
 >
 >   public void action() {
 >      greetings();
 >      talk();
 >      wrapup();
 >   }
 >
 >         .... more methods ...
 >   }
 >}
 >
 >
 >You might wonder *where* I took that from. The URL is:
 >
 >http://java.sun.com/whitePaper/java-whitepaper-5.html#HEADING5-23
 >
 >Are this specifications just make-up and optional? According to the
 >description these variables are used to clarify which variable one is
 >referring to. In
 >the Segment method, *this.segmentNumber*  would mean the *segmentNumber
 >instance variable* of this object, rather than the SegmentNumber parameter
 >to the Segment method.
 >
 >
 >Please let me know if I'm totally off track, or if I was right.
 >
 >Your reader,
 >
 >Michael Mehrle
 >--
 >
 >P.S. I will also post this message to comp.lang.java, so that everybody
 >can enjoy your reply (no I don't mean that maliciously!)
 >
 >
 >
 >                  ||||
 >                  o  o
 >____________OOOo___<>___oOOO_______________
 >     http://www.w3media.com/w3media
 >          michael@w3media.com
 >           Tel. 310.441.9599
 >           Fax  310.441.5919
 >
 >"One man's mundane and desperate existence
 >     is someone else's Technicolor."
 >            -Strange Days-
 >
 >
 >                  ||||
 >                  o  o
 >____________OOOo___<>___oOOO_______________
 >     http://www.w3media.com/w3media
 >          michael@w3media.com
 >           Tel. 310.441.9599
 >           Fax  310.441.5919
 >
 >"One man's mundane and desperate existence
 >     is someone else's Technicolor."
 >            -Strange Days-
 >
 >
 >-
 >This message was sent to the java-interest mailing list
 >Info: send 'help' to java-interest-request@java.sun.com
 >
-
This message was sent to the java-interest mailing list
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post