• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused with (this) !  RSS feed

 
krish reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class OneNumber
{
int n;
void setValue(int n)
{
this.n=n;
};
}

public class THAT
{
public static void main(String[] args)
{
OneNumber on = new OneNumber();
on.setValue(3);
System.out.println( on.n );
}
}


which is the current object in the above program and how can i make myself absolutely clear about "(this)" keyword

how about this program below

class iNeedHelp
{
int n;
void setMe (int m)
{
Help h = new Help();
h.setValue(this,m);
};
}

class Help
{
void setValue (iNeedHelp c,int h)
{
c.n=h;
};
}

public class SERIOUS
{
public static void main(String[] args)
{
iNeedHelp inh = new iNeedHelp();
inh.setMe(3);
System.out.println( inh.n );
}
}
this program drove me nuts help!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. Nicknames and "handles" aren't OK. You can change your display name here. I notice you've been reminded once already; thanks for your immediate cooperation!

In any case, any method is part of a class. Any instance method (i.e., any method that's not "static") must be invoked "on" an object. Inside the method "this" refers to that object, the one that the method is invoked "on". Generally, inside a method, "this" is the only name you have for that object.

Static methods -- methods that don't require an object to call them -- have no "this".
 
krish reddy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry about that, i have changed my name now, i am new to "this". still not clear on the second example
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi krish,

Your new name is a little closer, but it's still just one name. You need to have a first and last name. Please give it another try. Thanks for understanding.
 
Steve Morrow
Ranch Hand
Posts: 657
Clojure Spring VI Editor
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"which is the current object in the above program"

In the above program, you only have one object that is referenced by the variable named "on" in main(). When you call on.setValue(), "this" will refer to the same object as "on" does in main().

Now, let's look at what happens if you create a new object:

Now, if you call any methods using the variable named "another", "this" will refer to the same object as "another" does. This is why we say that "this" refers to the current object. The current object changes depending on how you call a method.

I agree that you should look at the link to the tutorial above. Hopefully it will clarify anything that doesn't make sense in my description above. If not, feel free to post more quesitons here and we will try to clarify.

Layne
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!