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






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

java web hosting

 

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved