This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

i need some help with 'this' keyword

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really confused on what exactly the 'this' keyword is doing like this.method() please help...thanks
 
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a piece of code would be nice to look at. "this" keyword represents a reference to the current object. "this" passed explicitly to all members of a class like member variables and methods , not to static methods( because they belong to a class not to an object). "this" is usefull in contructors where member variables are being overshodowed by parameter variables so "this" would destinquish between the two. Also used in constructors chaining where you can call one constructor. if you need to call a member method you don't need to explicitly say "this" because it's already there. I'd like to see a piece of a code maybe i am missing something.
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah. like Val says, 'this' is the current object, whos data is being accessed in a method.

so if we made an object of class SomeClass, like
SomeClass s1 = new SomeClass();
the code inside 'someMethod()' is saying
s1.Xpos = newX;
Hope that didn't cause more confusion that it alleviated. G
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are two major reasons for using the "this" keyword.
The first is if you need to make it clear whether you are talking about an instance variable or a local variable. Remember that a local variable only lasts the lift of the method, then it is gone. If you use the same name for both (as in Grant's second example) you use the this to clarify the instance variable.

this.x = x is like saying "set the instance variable x equal to the local variable x".
The other reason for using the this keyword is to call another constructor within the same class.
For instance the Color class has many constructors, depending on what parameters you feed in. One of the constructors takes in 3 values for the red, green and blue combination to create the color. Another constructor takes in the red, green and blue values AND a value for intensity.
The second constructor does not want to duplicate the code in the first, so on the first line it makes a call to the first constructor which creates the color for it,

then the second constructor continues on to modify that color with some intensity.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"this " gives the refernce of the class in which it is being called
calling variable.For ex
class Thistest
{
int i= 10;
Thistest()
{
this(4); //(1)
}
Thistest(int j){}
void method(){
int i=15
System.out.println(this.i); //(2)
}
void amethod()
{
System.out.println("amethod");
this.method(); //(3)
}
}

At (1) "this" will transfer the control to next constructor with
int as the argument.(matching constructor)
At(2) "this" will print 10
At (3) "this" will call the "method" of class Thistest
try this in many other ways Take the help of Khalid Mughal too.

I think it makes it clear

Thanks
Steven

 
Ranch Hand
Posts: 358
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cindy has very well explained the reasons for this keyword.
To make the concept of local and instance variable more clear try this code.
class ClassName
{
private int value1=0;
private int value2=0;
public void method1( int value1, int value2 )
{
this.value1 = value1;
this.value2 = value2;
}
}
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this basically contains the current mem address of your object.
for eg:
create an object of class a
and inside the constructor for a try printing the value of this.
u can also call another constructor from the first constructor.
but this should be the first statement in your constructor
bye
 
Cindy Glass
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to be clear - there ARE other reasons for using the "this" word, it's just those 2 above are the most common.
To start a thread:
Thread mythread=new Thread(this);
System.out.println(this); //takes any object and calls the toString method on the WHOLE thing (prints all the contents of the variables etc.)
Also in drawing image on an applet
g.drawImage(img,x,y,this);
etc.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic