jeudi 9 mars 2017

How can I get the name of a class, with an static Method ( not with Exception )

This is my situation:

I have to write a method wich can be called in any class to get the name of the class wich is calling the Method. The return Type should be the Class, so that I can use it for another Method. So that is what I have right now, but I think its not a clean code. Maybe there is way to use

public class Vlogger{
public static Class getInstance() throws ClassNotFoundException {
    String className =new Exception().getStackTrace()[1].getClassName();
   return Class.forName(className);}

}





Aucun commentaire:

Enregistrer un commentaire