A Brief Discussion of Static Methods
So, up until now every time we have invoked a method, we have invoked it ON an object. That is, we make an object,Fraction f = new Fraction(3, 4);and then we do something with that object:
f.decimalValue();
But in the last step, we simply made a call to Math.pow()
without making an object first.
The reason that works is the methods in Math are declared as static
. Static means you can make function calls WITHOUT an actual object. So it makes sense that a method that squares something doesn't need to operate on a particular object, whereas a method like decimalValue()
must be called on a SPECIFIC fraction.
Note that because static methods do not refer to an object, they can't access any instance variables except those that are declared as static.