If I have the following:
class A {
public A() { }
public static void foo() { System.out.println("foo() called"); }
}
public class Main {
public static void main(String [] args) {
A a = new A();
a.foo(); // <-- static call using an instance.
A.foo(); // <-- static call using class
}
}
Are there any problems that may arise from calling foo() using an instance? Does the JVM treat the first call to foo() exactly as a static method, or is there some technical subtlety?
Its very easy to introduce subtle logic errors by calling static methods from instances. Case in point, this doesn’t do what you think it does:
sleepis a static method which pauses the currently executing thread, not the thread instance.