Solved

what is "this." and what it does?

Posted on 2002-05-27
15
222 Views
Last Modified: 2010-03-31
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
0
Comment
Question by:garywhw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7036671
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

0
 

Accepted Solution

by:
jsharsha earned 25 total points
ID: 7036678
'this' refers to the current instance of the class.
Your class B may look like this

public class B{
   String userid;
   String password;

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

   public static void main(String args[]){
      B b = new B();
      int i = b.getprint("abc","xyz");
      System.out.println("this userid "+b.userid);
  }
}

In the getprint() method you are already having two local variables 'userid' and 'password', now if you want to refer to the instance variables of class B (which have the same names), you have to use 'this', which means that you are referring to the variables of the current instance.
   Also in the main method, it will output the value of the userid that you had passed.
   One of the other things to be noted is, you cannot use 'this' inside static methods, because static methods relate to a 'class' and not to a single instance.
0
 

Author Comment

by:garywhw
ID: 7036687
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
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Venci75
ID: 7036691
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
0
 

Author Comment

by:garywhw
ID: 7036728
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
0
 

Author Comment

by:garywhw
ID: 7036744
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`
0
 
LVL 9

Expert Comment

by:Venci75
ID: 7036765
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



0
 
LVL 9

Assisted Solution

by:Venci75
Venci75 earned 25 total points
ID: 7036766
class XXX {

String uid = "1"; // declare the class member variable
public void setUid(String x) {
 this.uid=h;
}
public void print(String uid) { // the method local variable
 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");
}
}

0
 

Author Comment

by:garywhw
ID: 7036786
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

0
 
LVL 9

Expert Comment

by:Venci75
ID: 7036795
sorry - the method should be:
...
public void setUid(String x) {
this.uid=x; // replace 'h' with 'x'
}
...
0
 

Expert Comment

by:ahmed_pro
ID: 7036881
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.
0
 

Expert Comment

by:allanhicks
ID: 7037075
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.

 
0
 

Author Comment

by:garywhw
ID: 7038124
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

0
 
LVL 35

Expert Comment

by:girionis
ID: 8798057
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
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question