File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Use of keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Use of keyword "this"" Watch "Use of keyword "this"" New topic
Author

Use of keyword "this"

Gary Farms
Ranch Hand

Joined: Jun 24, 2000
Posts: 57
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

Joined: Nov 06, 2001
Posts: 4
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

Joined: Sep 29, 2000
Posts: 8521
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


"JavaRanch, where the deer and the Certified play" - David O'Meara
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Jun 24, 2000
Posts: 57
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

Joined: Sep 29, 2000
Posts: 8521
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

Joined: Oct 31, 2001
Posts: 54
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


PAS<br />peter@panvox.net<br />2b || !2b
 
jQuery in Action, 2nd edition
 
subject: Use of keyword "this"
 
Similar Threads
this keyword and JQPlus Qs
What is the deal with Constructor
Concstructors withing constructors
Object Creation in Java
Constructor