public class stackofints<Item>
{
private static node topofstack=null;
private static class node{
Item item;
node next;
}
public static void push(Item item){
node oldtopofstack=topofstack;
topofstack=new node();
topofstack.item=item;
topofstack.next=oldtopofstack;
}
public static int pop(){
Item item=topofstack.item;
topofstack=topofstack.next;
return item;
}
public boolean isEmpty(){return topofstack==null;}
public static Item size(){
Item i=0;
node iterate=topofstack;
while(iterate!=null)
{
iterate=iterate.next;
i++;
}
return i;
}
public static void main(String[] args)
{
push(1);
push(2);
push(3);
System.out.println(size());
}
}
In the above code I am trying to use generic type but I get a compilation error:
File: D:\Java Code\stackofints.java [line: 6]
Error: non-static type variable Item cannot be referenced from a static context.
Can somebody help me resolve this issue. Thanks