This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes When does 'this' become availible for use? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "When does Watch "When does New topic
Author

When does 'this' become availible for use?

Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
When does 'this' become availible for use in the local class?
I think its.. <ahem>
after the last locally chained constructor has completed the implicit or
explicit call to the superclass constructor.
I say so because, if I have it right, 'this' cannot be used in the superclass constructor, yet it can be used straight afterwards - in the instance initializer (that executes just after the "implicit or explicit call to the superclass constructor"), as well as in the rest of the constructor body.
...or am I just mumbling on like drunk?


<img src="cool.gif" border="0"> <img src="graemlins/beerchug.gif" border="0" alt="[beerchug]" /> <br />SCJP 1.4
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

My experiments suggest this is available even before the return from the superclass constructor.
this is available in the superclass when the fields are being initialized, the instance initializers are being executed and the constructor is being executed.
[ January 15, 2004: Message edited by: Marlene Miller ]
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
The argument to the superclass constructor is a static context. this is not available as an argument to the invocation super(arguments).
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
Isn't is frustrating when people do not answer the question you asked!
You asked when is this available in the *local* class, and I am going on and on about this in the superclass.
Let me try again. My experiments show that this is available in the local class when the fields are being initialized and the instance initializers are being exectuted, which occurs just after the return from the superclass constructor and before the execution of the body of the local constructor.
So I agree with you, this is available after the last local constructor has invoked super(). But I disagree with you, because this can be used in the superclass constructor.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391

One thing that used to confuse me is that the compile-time type of this is the type of class in which the keyword this occurs.
When this is used in superclass A, the compile-time type of this is A. Even though the compile-time type of this is A, at run-time, this references a B object. So in superclass A, this can be cast to B.
(Of course, if you tried to instantiate A, as in new A(), the cast to B would fail at run-time.)
[ January 15, 2004: Message edited by: Marlene Miller ]
Jim Crawford
Ranch Hand

Joined: Sep 08, 2002
Posts: 127
Originally posted by Marlene Miller:
Isn't is frustrating when people do not answer the question you asked!

No problemmo!

So I agree with you, this is available after the last local constructor has invoked super(). But I disagree with you, because this can be used in the superclass constructor.

That was maybe a typo. It was related to a question I was looking at and I meant that this (and super, for that matter) cannot be used in the *invocation* of the superclass constructor, as a parameter.
Thanks for all the effort! I appreciate it.
... and from such a VIP around here as well! Wow.
Marlene Miller
Ranch Hand

Joined: Mar 05, 2003
Posts: 1391
I meant that this (and super, for that matter) cannot be used in the *invocation* of the superclass constructor, as a parameter.

Hi Jim. Just to make it official, you can find the rule in the JLS 8.8.5.1 (and also a list of static contexts in JLS 8.1.2).
An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When does 'this' become availible for use?
 
Similar Threads
One question about constructor
superclass constructors
about Inheritance
this() and super() calls
Another question on constructors