Showing posts with label prettycode. Show all posts
Showing posts with label prettycode. Show all posts

Friday, December 05, 2008

Java Pretty code II

Another piece of pretty code:

public class MyClass{
 public MyClass() {
 }

 public MyClass(String iClass, String projectPath) {
  /* Instantiates MyClass */
  MyClass obj = new MyClass();
  obj.create(iClass, projectPath);
 }
}

Why to create an object of the same class to call the method on it?

To the next piece of pretty code!

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!