Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What object initiated the current object

Status
Not open for further replies.

raypru

Technical User
Apr 9, 2002
26
0
0
GB
If Object A initiates Object B, how can I tell from within Object B that Object A initiated Object B without actually passing in the object name.

 
I am not sure if I understand your post completely, if you post some type of code example that would be more clear. From what I think you are trying to say, I don't understand why it would be important for Object B to care about which class has instantiated it.
gonna need more info.
 
Hi

Thanks for taking the trouble to respond. I have many classes that could call a particular class (SecondClass below) and from within the class (SecondClass) I want to know which of the other classes called it. I don't want to pass in a parameter to SecondClass giving the name of the calling class:



public class FirstClass {

SecondClass s = new SecondClass();
}

public class SecondClass {


// In here I want know which class called this class (FirstClass or ThirdClass)

}

public class ThirdClass {

SecondClass s = new SecondClass();
}
 
AFAIK, that's no possible. At least on standard Java. I guees you will have to use the parameters option.

Cheers.

Dian
 
Raypru,

i think that I have to agree with Diancecht. I have never heard of this being done before. But I am curious as to what purpose this would serve if it were possible? Are you trying to establish some type of logging capability? It sounds like you are somewhat familiar with Java, so if you wanted FirstClass (or ThirdClass) to only inherit certain attributes of SecondClass you could have an overloaded constructor...
Good luck!
 
I'm also curious about the purpose of knowing the, let's call it "caller". Maybe if you explain us what are you trying to do we can give you a hint.


Cheers.

Dian.
 
Impossible does not exist :

public class SecondClass {

public SecondClass () {
Exception e = new Exception("xxx");
StackTraceElement[] ste = e.getStackTrace();
System.out.println("Calling class = " + ste[1].getClassName());
}
 
Thanks for the input. hologram's example is what we are looking for but we are restricted to version 1.3.8 and the StackTraceElement is new from 1.41.

In answer to Diancecht, we are a team of 4 who are all fairly new to Java and we are developing a generic interface that will sit between two different systems but could also be plugged into any other system, if need be.

Transactions that will pass through the interface could initiate any number of the 8 classes in the system in any order, similar to a workflow. The 8 classes each have a configuration file. One of the classes is core to the system and needs to identify which class called it (so that it can look at the configuration file relevant to the calling class), and this could be any of the other 7 in any order.

As explained earlier, we do not want to pass in a parameter or have one configuration file.
 
Try this then :

Code:
try {
   boolean b = true;
   if (b) {
     throw new Exception();
   }
} catch (Exception e) {
   e.printStackTrace(System.err);
}
 
Why not use an listener interface to provide a call back mechanism. It could then report the parent class via this method?
 
Because he doesn't know the parent(sic) or rather invoking class ...
 
To raypru,

(I don't see why you don't want to pass the identification of the calling class into the newly instantiated class)

However if the only reason is that the newly instantiated class "needs to identify which class called it (so that it can look at the configuration file relevant to the calling class), " then why not just get the calling class to pass in its corresponding configuration class during instantiation.

Cheers

Lentil

 
Ha ! , after all that, I just had to the same thing. The reason being, we have custom connection pool, and wanted to know which JSP/Servlet invoked the JDBC Statement object - to aid in potential connection leaks, table locks and other problems. So, this is how we did it :

Code:
String caller = "";
try {
	throw new Exception();
} catch (Exception e) {
	StackTraceElement[] ste = e.getStackTrace();
	//e.printStackTrace();

	for (int j = 0; j < ste.length; j++) {
		if (this.getClass().getName().equals(ste[j].getClassName())) {
			// The instantiating object will always be one element above this class ...
			this.caller = (ste[j+1].getClassName() +&quot;.&quot; +ste[j+1].getMethodName() +&quot;, line : &quot; +ste[j+1].getLineNumber());
			break;
		}
	}
}
 
sedj :

The post requires a means to call methods DYNAMICALLY - not hard-code switch/if statements !!!

sorry -

The post requires a means to call 1.3.8- METHODS - not 1.4.x statements !!!

 
It looks like we are trying to do the impossible for the Java version we are at so we have redesigned our app so that we do not need to know the previous class.

Thanks to everyone who has contributed to this issue.
 
stefanwagner :

re your non -consturctive comments. whatever.

I posted the solution that we use so that anybody else wishing to do the similar would see ONE way of doing it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top