• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Dout with this key word

 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I have taken this question from Marcus mock exams

public class Tong{
private Tong(int i){
}
private int i=10;
public static void main(String argv[]){
Tong t = new Tong(40);
t.go(99);
}
public void go(int i){
System.out.print(i);
System.out.print(this.i);
}
}

the output i got was 9910 how can it happen because i and this.i are same so i argument is shadowing instance variable i so waht ever is passed to method should be printed that is 9999 but the output i got was 9910 can any please explain what is the concept behind this.

Thanks
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For the method:

public void go(int i){
System.out.print(i); // Line 1
System.out.print(this.i); // Line 2
}

The value of "i" in Line 1 refers to the argument (int i). The value of 99 was passed to the parameter i, so it prints 99.

The value "this.i" refers to the instance variable i which has been declared with a value of 10.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i or this.i are same so it should print 9999 or 1010 but why is it printing 9910 even in method invocation if i say a() or this.a() are both same am i correct.Please clarify

Thanks
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No they are not the same.

The formal parameter named i shadows the instance variable named i.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Formal parameters are also part of local variable correct so does it mean to say that this will refer to instace variables and methods only and not to local variables with in the method.

Thanks
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi can any one help me in understanding this problem
 
Ranch Hand
Posts: 332
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Subu Mhathma:
Formal parameters are also part of local variable correct so does it mean to say that this will refer to instace variables and methods only and not to local variables with in the method.

Thanks



"this" refer to instance variables and methods, you can't use to it to refer to local variables in method.
 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok
the variable i=10 here is a class variable.......
secondly "this" keyword is use to access "class variables as well as methods"
not the variables of the method where you are using it.
 
Chandra shekar M
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks every one I understood.

Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will see the use of this in methods such as setSomething:



this.something is used to enable the setting of the instance variable something from the local variable something.
 
This cake looks terrible, but it tastes great! Now take a bite out of this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!