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?
1 Answer 1
First of all you should know more about static and non-static methods:
A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can however be called both on the class as well as an object of the class. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated. A static method is shared by all instances of the class. These are some of the basic differences. I would also like to point out an often ignored difference in this context. Whenever a method is called in C++/Java/C#, an implicit argument (the 'this' reference) is passed along with/without the other parameters. In case of a static method call, the 'this' reference is not passed as static methods belong to a class and hence do not have the 'this' reference.
Reference:Static Vs Non-Static methods
How does this work? is
myClassInstancestill somehow there because at that point I'm still insidemyMethod()?
myMethod is an instance method that belongs to myClassInstance. so when you call myMethod() inside myMethod() it's equivalent to this.myMethod()
Would it be better to have
static MyClass myClassInstance = new MyClass()at class level and then callmyClassInstance.myMethod()every time?
When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member. A static member is shared by all objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present. For more information visit Static Variables : Good or Bad?.
Comments
Explore related questions
See similar questions with these tags.
myMethod()is equivalent tothis.myMethod(). Inside myMethod(), which is an instance method,thisexists: it's the current object. Inside main(), which is a static method, there is nothis. So the call doesn't make sense.