Java Reference
In-Depth Information
We can remove all static functions from the scope of the
List
body and attach
them to, say, a general-purpose class called
Toolbox
as follows:
Program 8.7
Linked list with static functions attached to a
Toolbox
class
class
List
int
container ;
List next ;
// Constructor List(head, tail)
List(
int
element ,
List
tail )
{
this
.container=element;
this
.next=tail;
}
}
class
Toolbox
{
static int
head( List l i s t )
{
return
list . container ;
}
static
List insert (
int
s , List
list )
{
return new
List(s , list ) ;
}
Then we could have created linked lists and called various related static
functions by invoking static functions of the
Toolbox
class. For example, let us
consider the following code snippet:
class
List
int
container ;
List next ;
// Constructor List(head, tail)
List(
int
element , List tail )
{
this
.container=element;
this
.next=tail;
}
}
class
Toolbox
{
static int
head( List l i s t )
{
return
list . container ;
}
static
List insert (
int
s , List
list )
{
return new
List(s , list ) ;
}
}
class
StaticFunction
{
public
static void
main( String
[ ]
args )
{
List myList=
new
List (3,
null
);
myList=Toolbox . insert (6 , Toolbox . insert (4 , myList) ) ;
Search WWH ::
Custom Search