Java Reference
In-Depth Information
System . out . println ( "Head:" +Toolbox . head(myList) ) ;
}
}
Compiling and executing the program returns the expected result:
Head:6
Note that all usual mathematical functions such as exp ( Math.exp ), log
( Math.log ), ( Math.sqrt ), ... are defined similarly, as static functions, in
the toolbox class Math . We can thus imitate this library behavior of classes by
attaching common functions to a Toolkit class:
Program 8.8 Static functions attached to a library class
class Toolkit
static final double PI=3.14;
static double Square( double x)
{ return x x; }
static double Cubic( double x)
{ return x x x; }
}
class StaticFuncStyle
{
public static void main( String [ ]
s )
double radius =0.5;
double volSphere =(4/3.0) Toolkit .PI Toolkit .Cubic( radius ) ;
double areaDisk=Toolkit .PI Toolkit . Square( radius ) ;
}}
A static function not accessing static class variables can therefore be attached
to any class 3 but requires us to get as arguments the objects on which it
processes on. The advantage of attaching all static functions to the same class
as its argument object is that we can remove the class name. That is, if class
insert were attached to the List class, then instead of having the instruction
myList=Toolbox.insert(6,Toolbox.insert(4,myList)); , we would have
simplified it as myList=insert(6,insert(4,myList));
On the other hand, a data-centric view will consider the object List on which
we can apply various processes called methods . These object methods do not
require us to give as an argument the object itself as it is understood that
these methods can access the object fields provided the function is a non-static
3 And can thus be loosely called class function .
 
 
Search WWH ::




Custom Search