5 Replies - 389 Views - Last Post: 14 October 2009 - 09:30 PM Rate Topic: -----

#1 Ixir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-October 09

this. statement

Posted 14 October 2009 - 08:48 PM

Hello, as I am steadily adjusting to java and starting to compose classes. I am having trouble with the this. statement. Ive searched and read up on the java site, as well as read the section on this. statement in absolute java and apparently am not making the connection on what it actually does. If anyone would be so kind to either explain to me its function or point me to a web source I can read up on it.

Any advice is greatly appreciated.

Thanks,

Is This A Good Question/Topic? 0
  • +

Replies To: this. statement

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9153
  • View blog
  • Posts: 33,961
  • Joined: 27-December 08

Re: this. statement

Posted 14 October 2009 - 08:56 PM

Basically, you use the keyword this for one of two main reasosns.

One- You have a variable that is shadowed by a parameter or local variable. The keyword this refers to the global variable and the other field is the local variable as shown below:
class A{
   int a;
   public A(int a){
	  this.a = a;
	}
}



The other reason to use the keyword this is to call another constructor, like so:
class Rectangle{
   public int width, height, x, y;
   public Rectangle(){
	   this(0,0,0,0);
	  }
	public Rectangle(int width, int height, int x, int y){
		  this.width = width;
		  this.height = height;
		  this.x = x;
		  this.y = y;
		 }
}


As you can see, the constructor w/o parameters makes a reference to the other constructor using the keyword this in order to set the default values to 0.
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: this. statement

Posted 14 October 2009 - 09:02 PM

this makes the difference between instance variables and parameter variables:

class Rectangle {
   int x, y, w, h;	   <---- these are instance variables proper to the class

   // constructor
   Rectangle(int x, int y, int w, int h) {
	  this.x = x;

the statement this.x = x; means

 have the instance variable x of the class Variable the one pointed bt <----
to have the value x received as parameter in the constructor statement Rectangle(x, y, w, h) {




You can do

class Rectangle {
   int x, y, w, h;	   <---- these are instance variables proper to the class

   // constructor
   Rectangle(int a, int b, int c, int d) {
	  x = a;


Here there is no ambiguity... there is only one x and one a
so the a value received as parameter is assigned t the instance variable x
but Java programmers like to have the parameters variable name to match the instance variable name so you have to say
this.x <--- the class x
= x <--- the parameter
Was This Post Helpful? 1
  • +
  • -

#4 scrat  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 18
  • Joined: 30-June 09

Re: this. statement

Posted 14 October 2009 - 09:03 PM

It basically means that an object has a reference to itself, which can be useful for clarifying scope of a field.

public class Bob {

	private String x;

	public String getX() {
		return x;
	}

	public void setX(String x) {
		this.x = x;
	}
}



In the above, this. is used to indicate that this.x is referring to the class member x, not the setX(x) argument. Java searches at method level first and if it can not fine something there, it looks at class members.

Hope that helps.
scrat
Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8066
  • View blog
  • Posts: 31,309
  • Joined: 06-March 08

Re: this. statement

Posted 14 October 2009 - 09:04 PM

Sorry Macos .... 2 cross posts tonigh
Was This Post Helpful? 1
  • +
  • -

#6 Ixir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 08-October 09

Re: this. statement

Posted 14 October 2009 - 09:30 PM

:^: Thank you guys for the help. I understand now, will just take some practice utilizing it to fully comprehend. Thanks for the code to, being able to see what is happening helped.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1