I understand the "Cannot make a static reference to the non-static method" error, but I came across with this:
public class MyClass {
public static void main(String[] args) {
MyClass myClassInstance = new MyClass();
while (true) {
myClassInstance.myMethod();
myMethod();//Cannot make a static reference to the non-static method myMethod()
}
}// END main
void myMethod() {
try {
//Stuff
}
} catch (Exception e) {
myMethod();
}
}// END myMethod
}// END MyCLass
I can't just call myMethod()
from main
but I can do it from inside the method itself (in this case I want to call myMethod()
again if an exception happens).
How does this work? is myClassInstance
still somehow there because at that point I'm still inside myMethod()
?
Would it be better to have static MyClass myClassInstance = new MyClass()
at class level and then call myClassInstance.myMethod()
every time?