Link to home
Start Free TrialLog in
Avatar of garywhw
garywhw

asked on

what is "this." and what it does?

2 parameters were passed ("userid" and "password") from a.class to another b.class. at b.class, the code looks like below:

public int getprint(String userid, String password)
{
this.userid = userid;
this.password = password;
return 1;
}

Question: although i had been reading the book and internet and am still find it hard to understand what does the "this" here is doing? can someone please tell me in brief what is the "this." here is doing? ps: when i return to System.out.println("this userid = " + this.userid); it fails to display anything.


all experts, please advise :) thanks
garyW
Avatar of Venci75
Venci75

class XXX {
String userid; //1
String password; //2

public int getprint(
  String userid, //3
  String password //4
  )
{
this.userid = userid; // 1 = 3
this.password = password; // 2 = 4
return 1;
}

}

this can be used to access the class members. In this case you have no other way to access the "userid" member of the class, because if you use only "userid" in this method - you will refer the local method variable, passes as a parameter

ASKER CERTIFIED SOLUTION
Avatar of jsharsha
jsharsha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garywhw

ASKER

Sorry Venci75

i dont understand you. perhaps, it is quite hard for me to understand "this.".

quoted from your post:

class XXX {
String userid; //1
String password; //2

public int getprint(
 String userid, //3
 String password //4
 )
{
this.userid = userid; // 1 = 3
this.password = password; // 2 = 4
return 1;
}

}

question: what do you mean by >>this can be used to access the class members.>>? which class? without "this.", i thought we can also access b.class and execute its method "getprint()"?

>>In this case you have no other way to access the "userid" member of the class, >>
what do you mean by accessing the "userid"? i thought userid isnt a method, it is just a parameter, right?

i am sorry but please advise again. But, thanks for your help and i really appreciate it. thanks venci75

Regards,
garyW
the userid is also a variable - class member. The declaration od userid is:
class XXX {
String userid; //1
String password; //2

...

you also have a parameter of the getprint method with the same name:
public int getprint(String userid, String password)

when you use "userid" in your getprint method - this will refer to the parameter. If you want to use the "userid" variable of the class (declaration:
class XXX {
String userid; //1
String password; //2

...
) within the getprint method you can access it only by using:

this.userid
Avatar of garywhw

ASKER

Sorry Venci75

i dont understand you. perhaps, it is quite hard for me to understand "this.".

quoted from your post:

class XXX {
String userid; //1
String password; //2

public int getprint(
 String userid, //3
 String password //4
 )
{
this.userid = userid; // 1 = 3
this.password = password; // 2 = 4
return 1;
}

}

question: what do you mean by >>this can be used to access the class members.>>? which class? without "this.", i thought we can also access b.class and execute its method "getprint()"?

>>In this case you have no other way to access the "userid" member of the class, >>
what do you mean by accessing the "userid"? i thought userid isnt a method, it is just a parameter, right?

i am sorry but please advise again. But, thanks for your help and i really appreciate it. thanks venci75

Regards,
garyW
Avatar of garywhw

ASKER

sorry for the wrong posting above,

Hi venci and jsharsha
do you mean that since 2 parameters were named the same manner in 2 different methods of different classes. hence, it is only possible to refer to the "userid" of the b.class by typing "this.userid"?

ie, a.class and b.class both have "userid" and the way to identify the "userid" of the called method (b.class)'s "userid" is by stating "this.userid". is this what you mean?

pardon if i got you all wrong, but i really appreciate your assistance provided. please advise, thanks

God bless
garyW`
not exactly. What matters here is only the class that has the "getprint" method.
you have 2 userid variables - the first is a class member. This means that it is accessible by all methods of this class (XXX) and is declared in this manner:
class XXX {
String userid;
....

the seconds one is a parameter of the getprint method.

you access the value of the first by using:
this.userid
and the value of the second:
userid

to illustrate this, check this:

class XXX {

String uid = "1";
public void setUid(String x) {
  this.uid=h;
}
public void print(String uid) {
  System.out.print("this.uid = " + this.uid);
  System.out.println("  !! uid = " + thisuid);
}
public static void main(String[] arg) {
  XXX xx = new XXX();
  xx.print("A1");
  xx.setUid("UID");
  xx.print("uid");
}
}

the result will be:

this.uid = 1  !! uid = A1
this.uid = UID  !! uid = uid



SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of garywhw

ASKER

please pardon me for being quite slow in the comprehension of your posting.

what is the value of the h for? i am sorry but it is quite confusing to me. thanks for your assistance and i will looking into your coding and try to understand it more.. thanks alot

Regards,
garyW

sorry - the method should be:
...
public void setUid(String x) {
this.uid=x; // replace 'h' with 'x'
}
...
this is a pointer or handler to the object...so when i say this.x it means the i look at the my currnet instance i working in and i change the value of my object member variable
this.x=y
x is a member variable and y is a paramter sent to the function.
From your original example

public int getprint(String userid, String password)
{
this.userid = userid;
this.password = password;
return 1;
}

In line 1, you have userid and password which are arguments to your method.

In lines 3 & 4 userid and password the arguements are assigned to userid and password the class attributes.
If you didn't have 'this' the complier would not know which variables you were talking about as the recievers of the assignment userid the class attribute or userid the argument. 'this' means this class.


 Class B b;  // Class is created and userid and password have default values
 String useridTheArguement="FooYou";  //declare and initialize the strings
 String passwdTheArguement="FooMe";

 b.getprint(useridTheArguement,passwdTheArguement);
.
.
.
.
.

In getprint

int getprint(String userid,String passwd){
// At this point, b.userid and b.passwd are have the values
// set by b's constructor.
// userid and passwd are getprint's arguements have have the values of "FooYou" and "FooMe"

// When the next line is executed, b's userid (since the method is b's method or it is _this_ class's method) is set to "FooMe" or the value of the getprint's 1st argument.

this.userid = userid;

I hope that this helped. Java and C++ allow overloading of variable names. Which means that the same name can point to different memory locations depending upon the context in which the variable name is used. userid and password in your example, mean either the arguement to the method or the class attribute of the class. The class 'this' is a placeholder so to speak, a stand in for the name of the class instance that the method applies to. So that the compiler can resolve what you are trying to do.

Without 'this', the compiler would have difficulty deciding if you meant to change the userid and passwd in the class to the arguements, or replace the arguements values with the usedid and passwd in the class.

I hope this helped. I offer it as a comment since it's the same thing as the other respondents have already said. just
stated a little differently.

 
Avatar of garywhw

ASKER

Thanks everyone,

i guess i had come to understand what "this." is doing. "this." is a special keyword to represent the variable within its own class only. ie, this.variable in this.class, am i right?  :)

thank you so much. wow, it amazing that internet is such a great community for the contribution of everyone from everywhere in the world. Thanks God

God bless,
garyW

Avatar of girionis
No comment has been added lately, so it's time to clean up this TA.

I will leave a recommendation in the Cleanup topic area that this question is:

- split points between Venci75 and jsharsha

Please leave any comments here within the
next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer