If a method or field is declared static
, you access it
by using the class name rather than a reference to a particular
instance of the class. Therefore instead of writing
Car c = new Car("New York", 89.7);
double maximumLegalSpeed = c.getSpeedLimit();
you just write
double maximumLegalSpeed = Car.getSpeedLimit();
There does not even have to be an instance of a class in order to invoke a static method in the class.
Static methods may not call non-static methods or members of the same class directly. Rather they must specify which instance of the class they are referring to. Trying to call a non-static method or member is a very common compile time error. The specific error message generated by the javac compiler will look something like this
Error: Can't make static reference to method void floorIt() in class Car.
Of course the names and signature will be changed to match the specific method and class.