You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
General <strong>Java</strong> Questions I<br />
public abstract class D implements C {<br />
}<br />
-jim<br />
Is there a way to know from class X which class called the method foo()?<br />
If class A and class B are calling a method foo() on class X, is there a way to know<br />
from class X which class called the method foo() (they can be either A or B). I know<br />
that this can be done by capturing the stack trace and examining it, but that solution<br />
looks expensive as I have to create a new Throwable object every time and capture<br />
stack trace (And I do this quite frequently).<br />
Is there any other elegant solution to do this, any help and direction<br />
is appreciated.<br />
Answer: Pass a reference to the class to the foo() method.<br />
foo(Object x){<br />
System.out.println(x.getClass());<br />
}<br />
should do it.<br />
Why does this simple application never exit?<br />
public class UIQuitTest {<br />
public static void main (String[] args) {<br />
java.awt.Frame f = new java.awt.Frame();<br />
f.dispose();<br />
f = null;<br />
} // end of main ()<br />
}<br />
The application above never quits, is it a bug or a (mis)feature? Win98, JRE 1.3.0<br />
Answer: By creating an AWT object, you now have started the AWT thread. In order<br />
to end the application now, you have to do a System.exit(0) that will kill all<br />
non-daemon threads, including the AWT thread.<br />
Q: Is it possible to stop an object from being created during construction?<br />
For example if an error occurs inside the constructor (e.g. the parameters pass in<br />
were invalid) and I wanted to stop an object being created would it be possible to<br />
return null rather than a reference to a new object. (I know the term return is<br />
technically correct in this case but you know what I mean).<br />
Basically, is it possible to cancel object creation?<br />
Answer: Yes, have the constructor throw an exception. Formally, an object _will_ be<br />
created (since the constructor is a method invoked after the actual method creation),<br />
but nothing useful will be returned to the program, and the dead object will be later<br />
reclaimed by Garbage Collector.<br />
But the clean way is as another reply suggests, that you leave calls to the constructor<br />
to a static factory method which can check the parameters and return null when<br />
needed.<br />
file:///F|/350_t/350_tips/general_java-I.htm (16 of 31) [2002-02-27 21:18:17]