v tnx 4 yr feedings I'll reply in a while: the problem statement was how to get a string representation of a fully qualified class name stored in a class (that is, static) variable without having to type it.
Mr. Cristian D. Romanescu wrote: >> In my oppinion is a very bad software design to use Exceptions for >> normal operation...such as this. >> >> I would go for new MyObject().getClass().getName(); You're using a constructor, so you're typing the class name: if this were allowed by the problem statement, it would be useless, because I would simply accomplish the work writing: private static String className = "mypackage.MyClass"; instead of private static String className = new MyClass().getClass().getName(); In addition it's not always possible or convenient instantiating MyClass: it would be a very heavy object, it would perform some obscure operation, it should not be instantiated for several reasons, or it cannot be i.e. when it's an abstract class... The same holds true for Suresh Babu Koya who wrote: >> Why not MyObject.class.getName()? Atleast you need not create >> any object. Anyway using exceptions it's not as bad as might seem: they are very light objects. Usages similar to this can be found frequently in the literature (see Daconta: Java Pitfalls, first ed.) tnx again, Sergio > [EMAIL PROTECTED] wrote: > > > tnx a lot for the insight! > > You solved me a refactoring problem in a project with 3000 classes > > Only that I'm using the JDK 1.3.2, so I have a bit of more work than this. > > > > package unknown; > > > > import java.io.StringWriter; > > import java.io.PrintWriter; > > > > class WhoAmI{ > > private static String className; > > static{ > > StringWriter stackTrace = new StringWriter(); > > new Exception().printStackTrace( new PrintWriter( stackTrace)); > > className = stackTrace.toString(); > > className = className.substring( className.indexOf("at ") + "at > > ".length()); > > className = className.substring( 0, > > className.indexOf(".<clinit>")); > > } > > } > > > > > > > >>[EMAIL PROTECTED] wrote: > >> > >> > >>>How can a class discover in a static context what fully qualified name is its own? > >>> > >>>package unknown; > >>>class WhoAmI{ > >>>private static final className = ...????; > >>>} > >>> > >>>I can't use, of course, this.getClass().getName(). > >>> > >>> > > > > > >>What about 'String myName = new > >>Exception().getStackTrace()[0].getClassName();'? > >> > >>-mw > >> > >>___________________________________________________________________________ > >>To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > >>of the message "signoff SERVLET-INTEREST". > >> > >>Archives: http://archives.java.sun.com/archives/servlet-interest.html > >>Resources: http://java.sun.com/products/servlet/external-resources.html > >>LISTSERV Help: http://www.lsoft.com/manuals/user/user.html > >> > > > > > > ___________________________________________________________________________ > > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > > of the message "signoff SERVLET-INTEREST". > > > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > > Resources: http://java.sun.com/products/servlet/external-resources.html > > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html > > ___________________________________________________________________________ > To unsubscribe, send email to [EMAIL PROTECTED] and include in the body > of the message "signoff SERVLET-INTEREST". > > Archives: http://archives.java.sun.com/archives/servlet-interest.html > Resources: http://java.sun.com/products/servlet/external-resources.html > LISTSERV Help: http://www.lsoft.com/manuals/user/user.html > ___________________________________________________________________________ To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff SERVLET-INTEREST". Archives: http://archives.java.sun.com/archives/servlet-interest.html Resources: http://java.sun.com/products/servlet/external-resources.html LISTSERV Help: http://www.lsoft.com/manuals/user/user.html