7 Replies - 1258 Views - Last Post: 24 April 2009 - 12:32 PM Rate Topic: -----

#1 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 05-December 08

Anyone can help to explain how works the "this.variable"

Post icon  Posted 23 April 2009 - 03:14 PM

Please anyone can help me to understand how works the "this.variable" command on java.
Is This A Good Question/Topic? 0
  • +

Replies To: Anyone can help to explain how works the "this.variable"

#2 bonkerspr  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 92
  • Joined: 05-December 08

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 04:07 PM

what is the use of "this." on java and when we have to use it ?
Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 04:08 PM

Sure, say you have a class like the following:

class Demo {

    private int a = 5;

    void showOutput(int a) {
        System.out.println(a);
        System.out.println(this.a);
    }

    public static void main(String[] args) {
        Demo demoInstance = new Demo();
        demoInstance.showOutput(7);
    }

}


We have the instance variable "a" but in an act of terrible design, there is also a variable "a" local to the method. This hides the instance variable so the first line printed is "7". How do we get to the value stored in the instance variable? We use this.a

Hope that helps!
Was This Post Helpful? 0
  • +
  • -

#4 W3B5T4R  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 13-April 09

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 04:11 PM

You use "this" when you want to explicitly refer to an Object.
Was This Post Helpful? 0
  • +
  • -

#5 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 04:21 PM

View PostW3B5T4R, on 23 Apr, 2009 - 03:11 PM, said:

You use "this" when you want to explicitly refer to an Object.

Not neccisarily. this. means your referring to the variable of the class. It is as foley said, you may have a class variable the same name as the param name, so you would use this.classVariable and then param variable.
this. simply means it belongs to the Class. Can be an object, variable , ect.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 1997
  • View blog
  • Posts: 4,149
  • Joined: 11-December 07

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 04:53 PM

It can also be useful when an instance needs to pass itself as a method argument.
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Anyone can help to explain how works the "this.variable"

Posted 23 April 2009 - 05:40 PM

View PostW3B5T4R, on 23 Apr, 2009 - 03:11 PM, said:

You use "this" when you want to explicitly refer to an Object.

Yes... this references the object which is an instance of the class into which you are

class Abc {
	int x;

	void method(Abc obj) {
		  obj.x = 10;	// set to 10 the instance variable x of the Abc object passed as parameter
		  x = 5;		   // set to 5 the instance variable x of the object I am
		  this.x = 5;	 // same thing	but you need the "this" when ambiguous
	 }

	 void steX(Abc obj, int x) {
		   obj.x = x;	 // set the instance variable x of the Abc object passed as parameter to the value of x passed also as parameter
		   this.x = x;	// here the "this" is necessary so say that we want to set to the value of x passed as parameter the instance variable x of this object
	 }
}


This post has been edited by pbl: 23 April 2009 - 05:46 PM

Was This Post Helpful? 0
  • +
  • -

#8 rajibulAlam  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 24-April 09

Re: Anyone can help to explain how works the "this.variable"

Posted 24 April 2009 - 12:32 PM

As its mentioned in above comments, we use this to refer to the class variables. I normally use this keyword when I reuse the variable names.


Class foo_Area
{
	int height;
	int width;
	int area;
	
	public foo_Area(int height, int width)
	{
		this.height = height;
		this.width = width;
		this.area = height*width;
	}
	/*
		more codes get set here
	*/
	
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1