Why can't I assign "this" in a constructor?

T

Thinkit

It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).

I could just use a static method, but why don't I have the choice?
 
S

Silvio Bierman

Thinkit said:
It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).

I could just use a static method, but why don't I have the choice?

Why on earth would you want to assign a value to this. The 'this' reference
refers to the object a method is called upon which is not something that can
be changed in the method itself. C++ had an intermediate non-standard (and
very obscure) facility to assign to 'this' in a constructor to allow for
memory allocation overloading before explicit overloading of the 'new'
operator became available. Is that what you want to achieve?

If you want what is sometimes called a 'virtual constructor' you really need
some factory-like construct.

Silvio Bierman
 
N

nos

Thinkit said:
It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).

I could just use a static method, but why don't I have the choice?
Don't you at least have to do a cast? What if
this is a reference to an Integer, would this.add() even work?
 
H

hiwa

It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).

I could just use a static method, but why don't I have the choice?
'this' is a value, not a variable. In other words, it can't be an
lvalue of an expression.
 
T

Tor Iver Wilhelmsen

It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).

Because constructors are used from the outside: If the caller wants a
particular MyObject they can call the proper constructor.

An calling this() is't calling an _alternate_ constructor, but is
known as constructor chaining. The constructor you're in is still
_the_ constructor invoked.
I could just use a static method, but why don't I have the choice?

Because it's insane? You're trying to extend a constructor's role to
being a factory method, which is not what it's for: It's role is to
initiate *one* particular instance, referenced via the value this.
 
M

Mark 'Kamikaze' Hughes

Thinkit said:
It seems like a basic thing you'd want to do. It seems it only lets
you call an alternate constructor, this(...), as the first line in the
method. I want to be able to just say this=new MyObject(...) or
this=this.add(...).
I could just use a static method, but why don't I have the choice?

Because choice is bad, when there's already a right way to do it.
What are you trying to accomplish that isn't supported by this()?
 
T

Thinkit

nos said:
Don't you at least have to do a cast? What if
this is a reference to an Integer, would this.add() even work?

If this has the method add(), this.add() will work. Apparently, this
can't be assigned as an l-value, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top