Three things:
- The second has an additional challenge, which means that it can change the result. It may be bad, maybe not, but it is something to consider. See an example of how this might work.
- The second creates an additional object. This is bad.
- The second means that you are calling the method of the object, not the class itself, which confuses the people who read it. This is also bad. See an example of how this can be very bad!
Consider the following: reason 1:
class ClassName { static int nextId; static int m() { return nextId; } int id; ClassName() { id = nextId; nextId++; } public static void main(String[] args) { new ClassName(); new ClassName(); System.out.println(ClassName.m()); System.out.println(ClassName.m()); System.out.println((new ClassName()).m()); } }
Consider the following by adding to reason 2 as indicated in @emory:
class ClassName {
Consider the following: reason 3:
class BadSleep implements Runnable { int i = 0; public void run() { while(true) { i++; } } public static void main(String[] args) throws Exception { Thread t = new Thread(new BadSleep()); t.start();
source share