Solved

what is "this." and what it does?

Posted on 2002-05-27
15
218 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
15 Comments
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
'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
Comment Utility
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
 
LVL 9

Expert Comment

by:Venci75
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Assisted Solution

by:Venci75
Venci75 earned 25 total points
Comment Utility
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
Comment Utility
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
Comment Utility
sorry - the method should be:
...
public void setUid(String x) {
this.uid=x; // replace 'h' with 'x'
}
...
0
 

Expert Comment

by:ahmed_pro
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now