[3600] 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 (Pat Niemeyer)
Fri Nov 17 05:20:27 1995

Date: Fri, 17 Nov 1995 02:35:03 -0600 (CST)
From: Pat Niemeyer <pat@icon-stl.net>
To: Michael Mehrle <michael@w3media.com>
Cc: java-interest@java.Eng.Sun.COM
In-Reply-To: <199511170131.RAA20142@vv.val.net>



On Thu, 16 Nov 1995, Michael Mehrle wrote:

> 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; ?
>    }
> 
>    Segment(int number, String aPlace) {
>       this(number);
>       thePlace = aPlace;      // also here: this.thePlace = aPlace; ?
>    }

this.segmentNumber isn't necessary because segmentNumber isn't ambiguous.
The search for the variable begins in the method and then proceeds to
the class.

Here's another example:

class Foo {
	
	int number = 42;

	void bar {
		int number = 3;
		
		System.out.println( number );
		System.out.println( this.number );
	}
}

This prints:

3
42


Pat
-
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