user2720913
user2720913

Reputation: 29

Static Method error

public class Queue {

    public class ArrayQueue
    {
        Object store[];
        int front,rear;
        static final int MAX = 100;

        public ArrayQueue()
        {
            store = new Object[MAX];
            front = rear = 0;
        }

        public void EnQueue(Object o)        
        {
            if((rear +1)%MAX!=front)
            {
                store [rear] = o;
                rear = (rear + 1) % MAX;
            }

        }

        public Object dequeue() throws Exception
        {
            if( empty())
            {
                throw new Exception();
            }
            else
            {
                Object data = store[front];
                store [front] = null;
                front = (front+1)%MAX;
                return data;
            }
        }
    }

    public static void main (String args)
    {
        main();
    }

        public static void main()
        {
        String choice;
        Scanner input = new Scanner(System.in);
        System.out.println("A.EnQueue");
        System.out.println("B.DeQueue");
        System.out.println("C.Print");
        System.out.println("D.MakeNull");
        System.out.println("E.Empty");
        System.out.println("F.Full");
        System.out.println("G.Exit");

        choice = input.nextLine();
        switch(choice){
            case "A":
                System.out.println("Enter a character");
                char x;
                x= input.next().charAt(0);
                // ArrayQueue.EnQueue(x);
                System.out.println(x+"was added to the queue");

        }
    }
    }
}

I'm having trouble with static, what actually is a static method the does an error occur in line 76 which is the "ArrayQueue.EnQueue(x);" if a made the EnQueue function static there is also an error why is that? how can i fix this error. The error is non static method Enqueue (Object) can not be referenced from a static context

Upvotes: 0

Views: 229

Answers (2)

Justine
Justine

Reputation: 116

The main difference between static and non-static methods is that static methods belong to an entire class and non-static methods belong to a particular instance of that class. Typically non-static methods are methods that will in some way change the state of an object. In your case your enQueue() and deQueue() methods are both methods that will change the state of an instance of your ArrayQueue object therefore both methods should be non-static methods.

Now in order to call a non-static method to modify (change state of your object) you will need to first instantiate an instance of the ArrayQueue object then call the enQueue method on that particular object.

For Example:

ArrayQueue myQueue = new ArrayQueue();  //instantiate the object first

myQueue.enQueue(variable); //then access the non-static methods to act on that object

Upvotes: 2

Collin Grady
Collin Grady

Reputation: 2242

The message is abundantly clear; public void EnQueue is not a static method, so you cannot call it as one.

You cannot simply convert it to a static method because you are attempting to reference non-static variables in the class.

Upvotes: 6

Related Questions