• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

When does 'this' become availible for use?

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

reply
    Bookmark Topic Watch Topic
  • New Topic