0

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?

asked Dec 8, 2018 at 12:56
2
  • 2
    of course you can't. The other method is part of an instance of the class MyClass and not the class itself. You really should learn about class and object methods Commented Dec 8, 2018 at 12:57
  • 1
    myMethod is an instance method. Calling is using myMethod() is equivalent to this.myMethod(). Inside myMethod(), which is an instance method, this exists: it's the current object. Inside main(), which is a static method, there is no this. So the call doesn't make sense. Commented Dec 8, 2018 at 13:00

1 Answer 1

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 myClassInstance still somehow there because at that point I'm still inside myMethod()?

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 call myClassInstance.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?.

answered Dec 8, 2018 at 13:19
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.