tags:

views:

424

answers:

8

Why are the methods of the Math class static ?

+22  A: 

Because they don't require any internal state, but there are no stand-alone functions in Java -- every function has to be a member of a class. Thus, they're static.

mipadi
+2  A: 

Largely because these are utility methods, many of which can be applied to computations on primitives. The purpose of the class is in large part to offer a standard library of functions, many of which don't need to be applied directly to an object.

Rob Lachlan
+2  A: 

They are static because the methods do not rely on instance variables of the Math class.

William Brendel
and, they're not polymorphic/virtual.
ChrisW
They are static because they are declared as `static`. They *can be* static because they don't rely on instance variables and don't need to be overridden or called polymorphicly..
Stephen C
A: 

Static functions are "side-effect" free. There is no need to keep track of state variables when you are expecting a single result from computation.

ifwdev
Static functions are not per definition side-effect free.
eljenso
A: 

They can be invoked as if they are a mathematical code library.

Warrior
+5  A: 

When you have methods that don't really involve a state or a class (E.g., math functions, utilities on arrays, etc.), the class is just used to "group" them into a coherent location. So you list them as static (since they do not touch state) and since you never actually instantiate the class.

Uri
+2  A: 

Along with what everyone else has said... static methods are faster to call for 2 reasons:

  1. they are not polymorphic - so it is a special JVM instruction to run them
  2. they do not need to pass "this" as the first argument - not passing arguments means less items on the stack which means less time spent putting items on the stack
TofuBeer
A: 

They need to be static because numbers are primitives and not proper objects so they cannot be instance methods of the number types.

Now Java also has classes for boxing numbers, but it would be too inefficient to create an object for each mathematical operation.

In other languages, e.g. Smalltalk, numbers are objects and all arithmetic operations are instance methods.

starblue