I know that static variables are common for all classes and are the only instance. But why do we need static methods? And why can not we call non-static fields in static methods

Cannot make a static reference to the non-static field

And basically I don’t understand in which cases to use static ones ?!

  • one
    Doesn't the tutorial on java explain this with examples? - Enikeyschik February
  • @ Enikeyschik, on the contrary, I think, they read literature without independent practice of writing code. From here there are no situations when, for example, it is impossible to do without the same static method and it is not clear for a person why it is needed. - Vennic 6:49 pm
  • Your wording suggests that you do not understand the essence of classes. static variables are common to all classes - this is incorrect. are the only instance - this expression is devoid of meaning. Understand carefully what a class is and what is an instance of a class aka object. If you are reading a textbook, read more thoughtfully or try reading another. Try to write, compile and execute examples from the textbook, try to change them, see what happens and try to understand why. - m. vokhm

1 answer 1

Static variables and methods do not belong to specific objects of the class, but to the class itself. That is why it is impossible to call a non-static method from a static method (not tied to a specific object).

  • This is exactly allowed, only it would be more correct to use the class prefix before the call. - Roman C