Friday, December 05, 2008

Java Pretty code

Today, between a debug and another one I found this nice thing:

private VectorString> extractFields(Class classObj) {
 String className = classObj.toString();
 className = className.substring(6, className.length());

 Class obj = null;
 try {
  obj = Class.forName(className);
 } catch (ClassNotFoundException e1) {
  e1.printStackTrace();
 }

 ...
}

1) Please note the toString() call and then the extraction of the result cutting from the 6th character... But why don't simply to use the getName() method?
2) And at the end... Why to extract the name of the class and then reload the class by the extracted name to obtain...always the Class taken as parameter!

These are pearls folks!

No comments: