Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Use of keyword "this"

 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does a reference to the keyword "this" always call the current object constructor? From my understanding, sometimes it does and acts similar to "super" to call a constructor, and other times it just refers to the current object. Is this correct?
 
Ethan Knepp
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gary Farms:
Does a reference to the keyword "this" always call the current object constructor? From my understanding, sometimes it does and acts similar to "super" to call a constructor, and other times it just refers to the current object. Is this correct?

I have noticed that if I call "this" on an object that extends another class, i get the constructor for the current object and the extended object.
Even more fun comes when I call this on an object that extends another class, which extends another class, thus giving me the constructors of all three objects.
Did this answer your question?
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of the word "this" does not CALL any constructor. It just refers to the object that is the current object.
When you are in the middle of declaring a class (writing the code that defines its fields and methods) you can use the word "this" to mean that in the future, each time an instance is created and this code is executed - then you mean the instance of this class that is current at the time of execution.
However - it is true that when you create an instance of a class, each of the superclasses must be created from the top down before the current constructor is executed.
------------------
Cindy Glass
Sun Certified Programmer for the Java� 2 Platform
Co-author of Java 2 Certification Passport
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The use of the keyword "this" within constructor with appropriate parameters will call the constructor in the same class that has the corresponding/matching arguements.


------------------
Bosun
SCJP for the Java� 2 Platform
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh duh! You meant this(whatever) and I was focusing on this.something.
True - it will call the constructor with the signature that is specified.
 
Gary Farms
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy:
Thanks for your response. You said that "this" never calls a constructor. However, I'm still a little confused since on Page 40 of the book Just Java 2 by Peter van der Linden, it illustrates an example on the middle of the page showing the Fruit no-args constructor, issuing the command this(55,0) to call an args constructor of the same Fruit class.
This use of "this" is what led me to believe that "this" is also used to call constructors.

Originally posted by Cindy Glass:
The use of the word "this" does not CALL any constructor. It just refers to the object that is the current object.
When you are in the middle of declaring a class (writing the code that defines its fields and methods) you can use the word "this" to mean that in the future, each time an instance is created and this code is executed - then you mean the instance of this class that is current at the time of execution.
However - it is true that when you create an instance of a class, each of the superclasses must be created from the top down before the current constructor is executed.

 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yeah - we must have been posting at the same time (see above).
In one place *only* the use of the "this" word is allowed to be used to call a different constructor than the one that was invoked. That is on the first line of a constructor. And you only get ONE chance - so you gotta pick whether you want to make a call to another constructor of this class or a call to a specific constructor of the super class.
In this usage the syntax is always:
this(some signature); //or
super(some signature);
Any other place that you use the this word it is a reference to the current object as opposed to calling a different constructor.
 
Peter Simard
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, this forces the compiler to remain local in the scope search also. So use of this is often redundant, as the compiler searches local to global, but this can ensure that A) I definately wanted to be in this scope and to a lesser extend B0 provide for self-documenting code.
PAS
------------------
PAS
peter@panvox.net
2b | | !2b
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic