static Methods Cannot Directly Access Instance Variables and Instance Methods

A static method cannot access a class’s instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method. The this reference must refer to a specific object of the class, and when a static method is called, there might not be any objects of its class in memory.

Image Common Programming Error 8.5

A compilation error occurs if a static method calls an instance method in the same class by using only the method name. Similarly, a compilation ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.