Couple of Reflection Type Questions

P

Paul Joel

I suspect the answer to these questions is no, but I'll ask in case.

1. Can an object's constructor get a reference to the 'calling object'
without expliciting passing in 'this' as part of the constructor ?

2. Can an object's constructor get a reference to the Field object
representing the field that the new object will be assigned to if part of an
Object obj = new Object() call ?
 
D

Dale King

Paul Joel said:
I suspect the answer to these questions is no, but I'll ask in case.

1. Can an object's constructor get a reference to the 'calling object'
without expliciting passing in 'this' as part of the constructor ?

There is a way to get at the calling method, but should only be used for
debugging purposes and not relied on for production code. In 1.4 and later
you can get a stack trace from Throwable.

You should be passing it in. You may eventually want someone to call it and
not use this but a different object.
2. Can an object's constructor get a reference to the Field object
representing the field that the new object will be assigned to if part of an
Object obj = new Object() call ?

Definitely not.
 
M

Michael Borgwardt

Paul said:
I suspect the answer to these questions is no, but I'll ask in case.

1. Can an object's constructor get a reference to the 'calling object'
without expliciting passing in 'this' as part of the constructor ?

Only if the object being constructed is an instance of a non-static inner class
of the class of the object calling the constructor. Then you can use the syntax
OuterClassName.this
 
R

Roedy Green

1. Can an object's constructor get a reference to the 'calling object'
without expliciting passing in 'this' as part of the constructor ?

there is not necessarily a calling object. There is not necessarily
even any object involved in the parameters to create a new object.
There is not necessarily even a receiving object.

It is legit to say:

new Date();

all by itself in the middle of a static method -- no object anywhere
to be seen.

If you want the new object to use some data, the it has to be passed
as a parameter to the constructor, or the constructor has to go get
it, but it has to be able to figure out where to get it from using
only the state information available to it.

Cheaters would suggest looking at stack traces to figure out who
called you. The easy way is just to past a parent = this reference in.
 
R

Roedy Green

2. Can an object's constructor get a reference to the Field object
representing the field that the new object will be assigned to if part of an
Object obj = new Object() call ?

It won't necessarily be assigned anywhere. It may be mixed with all
kinds of other stuff and other objects before being assigned.

The whole idea of the subroutine it is keep it from peeking outside.
As soon as you allow such tentacles, then your code turns to a plate
of spaghetti.
 
C

Chris Smith

Michael said:
Only if the object being constructed is an instance of a non-static inner class
of the class of the object calling the constructor. Then you can use the syntax
OuterClassName.this

That gives a reference to the containing instance, not the "calling" or
creating instance. If it happens to also be the creating instance,
that's merely coincidence. The two could be different if you write, for
example:

bar = foo.new Bar();

So, while technically correct, your answer is approximately the same
thing as "only if you stored the caller in a public static member of an
unrelated class". That is, yes you can access a reference which could
be arranged to be the same as the creating instance, but it's not
because that's the creating instance; it's because you stored the
information somewhere else.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top