• Post Reply Bookmark Topic Watch Topic
  • New Topic

exact work of "this" keyword  RSS feed

 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just started to learn JAVA and am really enjoying it....

I understand what exactly does the "this" keyword does i.e.

1) keyword can be used to call in current class instance variable
2) used to invoke current class constructor and method
3) Can be passed as a parameter to both constructor/method
4) can be used to return current instance

I have written the below 2 programs to understand the difference of with/without using "this" keyword

Before using "this" keyword



output

0 null
0 null

after using "this" keyword


output

23 Java
20 Android

My question here is what exactly is happening here i.e. what exactly is the "this" keyword doing in both the programs.. Any help in this regard is much appreciated..

Thanks


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The difference is in the constructor. The constructor has two parameters, name and age. The class has two fields, also called name and age.

The important thing to realise is that the constructor parameters are different to the instance fields even though they have the same name. Java allows two variables to have the same name as long as they are not in the same scope. The constructor parameters are in the local scope (existing only inside the constructor), and the rule is that variables in the local scope hide the variables in the class and instance scope.

So in the first constructor when you say age = age, both reference to age refer to the constructor parameter. That statement has no effect at all.

To assign the value to the instance parameter you have to refer explicitly to the variable in the instance scope, which is what the 'this' keyword does.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the prompt reply mike....

if

age = age(referring to constructor parameter). here in the program the constructor parameters if they are considered to be local variables, the output of the first program here is that of default values of an instance integer and string variables, if the reference is to local variables(in this case the first constructor parameter) ain't is that the local variables do not have default values....? correct me if am wrong....
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor parameters do not have default values . They have the value that is passed in to them when the constructor is called. It's not possible to call the constructor without supplying values for all of the parameters.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try it on Eclipse, and you will get a warning about ineffectual assignments or something similar. Look for the little yellow icon a the left of the line.
What you are doing is setting the value of the parameter to the value of the parameter, and the fields retain their default values.

And welcome to the Ranch
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok

that means when i am saying age = age in the constructor, the the "age" parametered field is getting equalled to the default values of the instance variables of the class i.e. "age". But what is happening to the values that are being passed into ..


And Thanks Ritche..

 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:when i am saying age = age in the constructor, the the "age" parametered field is getting equalled to the default values of the instance variables of the class i.e. "age"

No, the value of the age parameter is set equal to the value of the age parameter. So, its value is unchanged.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:Ok

that means when i am saying age = age in the constructor, the the "age" parametered field is getting equalled to the default values of the instance variables of the class . . .
No. There is no such thing as a parameterised field (at least I think not). That is an ordinary field.
No. You are not setting the field to anything. You are simply leaving it alone and only setting the constructor parameter.
No. You are not using the value of the field in the constructor. Try adding the following line as the last line in the constructor.
System.out.printf("Values of constructor parameters: %d %s%n", age, name);
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick example with numbers might help:
So, instance variable "a" is 0 (default value). If you were use "this", "a" would be assigned to 4.
 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok

here is what i understand...

when am passing values in the main method to the constructor

in the constructor since instance variable age = age local variable of the constructor, both of them become equal i.e if am adding System.out.printf("Values of constructor parameters: %d %s%n", age, name); as mentioned by Darryl this shows the values that am passing on from the main method.

There is another method called display which has got

void display()
{
System.out.println(age+ " " +name);
}

when am trying to access this method via the object created am getting 0 null, 0 null as output that means the method has the default values of the instance variables i.e. 0 for integer and null for string. and since i did not instantiate the instance variables am getting the default values..

1st program

since i did not differentiate between instance and local am getting the default values printed since the method takes in the default values of instance variables

2nd program

since am distinguishing between instance and local variables

this.age = age is getting me the passed on values from the main method

age = age gets me the default values on the declared instance variables....

Hope i am correct.....

 
Amit Ghorpade
Bartender
Posts: 2856
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:age = age gets me the default values on the declared instance variables....

Not really. age=age assigns the method variable (the passed parameter) the same value it already has. So you are getting the default value of the instance variable because it is not assigned.
Remember local variables "shadow" instance variables with same names. "this" notation tells the compiler to use the instance version of the variable and not the local one.
Read about shadowing.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
krishnadhar Mellacheruvu wrote:since i did not differentiate between instance and local am getting the default values printed since the method takes in the default values of instance variables

No, that is not correct.

It would be clearer if you added a few statements to print the values inside the constructor. What you are missing is the fact that in your "before" program where you don't use this, the instance variable is getting shadowed by the parameter with the same name. So on lines 9 and 10, the values involved are NOT the default values. Rather, the values are still the ones you passed in from the main method, namely, 23 and "Java" for the first instance and 20 and "Android" for the second instance. However, the statements in the constructor do not affect the instance variables at all because the instance variables have been shadowed by the parameters with the same name.

In summary:

In the first program where you don't use this, the instance variables are shadowed so they are not affected by the assignment statements in the constructor. Both sides of the assignment statements in the constructor refer to the parameter. The reason that default values are printed when the display method is called is not because the constructor parameters are getting default values (they are not) but rather because there were no statements in the constructor that changed the value of the instance variables from their defaults to something else.

In the second program where you use this, the value of the instance variables are actually changed because the left side of the assignment refers to the instance variable and the right side refers to the parameter.

In both programs, the instance variables have their default values prior to the execution of the assignment statements in the constructor and the constructor parameters have the values that were passed in from the main method. However, only the second program changes the values of the instance variables to the values that were passed in to the constructor.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that shadowing occurs only because the constructor parameters have the same names as the instance variables. You can avoid shadowing by using different names for the constructor parameters. I have seen programmers do something like the following:

Here, the left side of the assignment refers to the instance variable. The right side obviously refers to the parameter.

This is not a common practice though. Using this is more idiomatic.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why not try like this

instance variable are the variable which(are inside class) are not defined in any method(Don't appear in method parameter too) .They take default value if no value is assigned to it
.In your case :

They are instance variable.

Local variable are all the variable which appears in the method(or appear as parameter). They give compilation error when no values is assigned to them.
javademos(int age,String name)
They are local variable.


now in your case
in the first code:
when you used



23 will go to age parameter of javademos constructor,
when you used


inside your constructor,then this happens:
age(parameter of javademos constructor)=age(javademos constructor);


while age (instance variable is not assigned)

now when you use

This wll print value of instance variable(age,name),which are not assigned,hence the default value




Now with your other code:
you used:

here "this" ensures you are initializing instance variable(age,name)

So this will not print default value..

Hope this helps



 
krishnadhar Mellacheruvu
Ranch Hand
Posts: 118
Android Java Objective C
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Man.. this took longer time to understand than i thought.

Thanks every one for helping me out in understanding "this" concept...
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!