Re: how to pass this before supertype constructor has been called
From: Dale King (kingd_at_tmicha.net)
Date: 12/03/03
- Next message: Denis: "reading a skingle key without pressing ENTER after"
- Previous message: James: "Re: unexpected exception when using JNI"
- In reply to:(deleted message) Paul J. Lucas: "Re: how to pass this before supertype constructor has been called"
- Next in thread: Paul J. Lucas: "Re: how to pass this before supertype constructor has been called"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 3 Dec 2003 12:48:16 -0500
"Paul J. Lucas" <pjl.removethis@removethistoo.mac.com> wrote in message
news:8pozb.32101$F%6.25951@newssvr29.news.prodigy.com...
On a side note, completely by coicidence, I downloaded txt2pdbdoc only to
discover that it was written by a certain Paul J. Lucas. I may convert it to
Java for my needs.
> Dale King <kingd@tmicha.net> wrote:
>
> > And no the this reference is not known until the constructor is called.
>
> Of course it is. It has to be. The constructor has nothing to
> do with it. For a class with neither base nor derived classes,
> the steps are:
>
> 1. Programmer calls "new SomeObject()"
> 2. JVM allocates memory from the heap for sizeof( SomeObject ).
> 3. Calls SomeObject() constructor with "this" set to point to
> memory allocated in step 2.
> 4. Programmer does stuff inside constructor code like
> initialize member variables.
And that is making some assumptions about how a VM works that may not
guaranteed by the spec.
> The only thing that is different if SomeObject has a base class,
> is that it is up to the *programmer* in step 4 to call super().
> The address of "this" MUST STILL BE KNOWN.
But is in an unknown state and is unsafe to use.
> > You have a chicken and the egg paradox
>
> No I don't. I can do what I want in C++ just fine. As long as
> I don't try to access anything *through* "this" I'm fine. It'd
> perfectly safe. It's 100% portable. It just works.
And the fact that C++ lets you access the pointer before it is initialized
does not mean the paradox doesn't exist. On the flip side C++ doesn't let
you access member functions polymorphically from a constructor. In this
regard C++ is safer than Java. Unfortunately that makes it much less
convenient.
> > and you think Java should let you get away with referencing an
unconstructed
> > object as a way to solve the paradox. You are wrong. Java should not let
you
> > do that.
>
> It's not a paradox. Sorry you just can't see that.
Yes it is. Sorry you just can't see that. Once again remove the whole
subclass thing and just look at it as two classes that want to have final
instance references to each other. I'll make them final to remove the
possibility of subclassing:
final class A
{
final B b;
public A( B b )
{
this.b = b;
}
}
final class B
{
final A a;
public B( A a )
{
this.a = a;
}
}
That is a chicken and egg paradox. There is absolutely no way that you can
create instances of these two classes so that they refer to each other. You
can create instances of these classes, but not so that they reference each
other.
You feel that you should be able to get around this paradox by subclassing
one of them, but that still does not eliminate the fact that the paradox
exists. C++ gives you a way out of the paradox, but the paradox still
exists.
I agree with Chris, that you can argue whether you should be allowed to get
the this pointer before the instance is initialized until you are blue in
the face, it doesn't change reality. The only reason you want it is because
you have created a paradoxical design. The solution is to fix your design so
that it is not dependent on the language allowing you to do things that are
inherently unsafe.
-- Dale King
- Next message: Denis: "reading a skingle key without pressing ENTER after"
- Previous message: James: "Re: unexpected exception when using JNI"
- In reply to:(deleted message) Paul J. Lucas: "Re: how to pass this before supertype constructor has been called"
- Next in thread: Paul J. Lucas: "Re: how to pass this before supertype constructor has been called"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|