1

I am new to Generics in java and I really need help in this code it is not compiling i dont know why!
The stack class is:

 public class GenericStack<Item>{
    public class Stack {

        private Node first=null;

        private class Node {
            Item item;
            Node next;
        }

        public boolean IsEmpty()
        {
            return first==null;
        }

        public void push (Item item)
        {
            Node oldfirst = first;
            first = new Node();
            first.item = item;
            first.next = oldfirst;
        }

        public Item pop ()
        {
            Item item=first.item;
            first=first.next;
            return item;
        }
    }
}

and here is the main

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    GenericStack<Integer> ob = new GenericStack<Integer>();
    ob.push(5);
    obpush(10);
    ob.push(15);
    while (!ob.IsEmpty())
    {
        int x=ob.pop();
        StdOut.print(x);
    }

  }
}

now the error is:

  The method push(int) isn't defined for the type GenericStack<Integer>

Where did i go wrong?! can anyone explain please to me

Thank you in advance

3
  • 3
    GenericStack doesn't have the method push, only the nested class Stack has it. Commented Feb 24, 2013 at 17:07
  • Because there is no method which accepts int argument ie push(int). Commented Feb 24, 2013 at 17:09
  • I think you are thinking about Autoboxing Commented Feb 24, 2013 at 17:09

5 Answers 5

3

Your GenericStack class has no methods. Get rid of the nested class structure and use the generic type parameter for Stack directly:

public class Stack<Item> {

    private Node first=null;

    private class Node {
        Item item;
        Node next;
    }

    public boolean IsEmpty()
    {
        return first==null;
    }

    public void push (Item item)
    {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }

    public Item pop ()
    {
        Item item=first.item;
        first=first.next;
        return item;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2
class GenericStack<Item>{
    class Stack {

        private Node first=null;

        private class Node {
            Item item;
            Node next;
        }

        public boolean IsEmpty()
        {
            return first==null;
        }

        public void push (Item item)
        {
            Node oldfirst = first;
            first = new Node();
            first.item = item;
            first.next = oldfirst;
        }

        public Item pop ()
        {
            Item item=first.item;
            first=first.next;
            return item;
        }
    }
}

public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    GenericStack<Integer> ob = new GenericStack<Integer>();
    GenericStack<Integer>.Stack st=ob.new Stack();
    st.push(5);
    st.push(10);
    st.push(15);
    while (!st.IsEmpty())
    {
        int x=st.pop();
//        StdOut.print(x);
        System.out.println(x);
    }

  }
}

You are calling methods of the inner class. So that using object of outer class you cannot directly call the methods of the inner class. See above code for that.

Hope this helps.

Comments

2

Because method push is defined in class GenericStack.Stack, not GenericStack. To make it work replace

GenericStack<Integer> ob = new GenericStack<Integer> ();

with

GenericStack<Integer>.Stack ob = new GenericStack.Stack ();

1 Comment

Since it is not a static inner class you need to have a live instance of the enclosing class. so that your second statement should be GenericStack<Integer>.Stack st=outerClassObject.new Stack();
2

The main issue with your code is that you mixed 2 public classes, just changed a little your code, have fun !!

GenericStack.java

public class GenericStack<Item> {

    private Node first = null;

    private class Node {
        Item item;
        Node next;
    }

    public boolean IsEmpty() {
        return first == null;
    }

    public void push(Item item) {
        Node oldfirst = first;
        first = new Node();
        first.item = item;
        first.next = oldfirst;
    }

    public Item pop() {
        Item item = first.item;
        first = first.next;
        return item;
    }

}

TestGenericStack.java

public class TestGenericStack {

    public static void main(String[] args) {

        GenericStack<Integer> ob = new GenericStack<Integer>();
        ob.push(5);
        ob.push(10);
        ob.push(15);
        while (!ob.IsEmpty()) {
            int x = ob.pop();
            System.out.println(x);
        }

    }
}

Comments

1

Get rid of the extra coating of class Stack from GenericStack.

Thanks!

2 Comments

This is unnecessary. There is such a thing as autoboxing in Java, after all.
Not only is it unnecessary but it's also a waste of memory. new Integer(5) creates a new object while Integer.valueOf(5) or just 5 both allow for reuse of an existing instance of Integer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.