0
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

Andon M. Coleman
43.5k2 gold badges87 silver badges110 bronze badges
asked Sep 15, 2013 at 1:36
2
  • 1
    Would you mind renaming all your method names (both static and non-static) to lowercase initials? According to Java standards, method names like EnQueue() are strongly discouraged, use enQueue() instead. Commented Sep 15, 2013 at 1:48
  • 1
    A static method is one which can be called without having an object of the class to which it belongs. Your approach here should be either to create an object of class ArrayQueue, or declare the method static. Commented Sep 15, 2013 at 1:57

2 Answers 2

6

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.

answered Sep 15, 2013 at 1:39
Sign up to request clarification or add additional context in comments.

Comments

2

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
Bad Wolf
8,3495 gold badges36 silver badges44 bronze badges
answered Sep 15, 2013 at 2:45

1 Comment

where should i insantiate the object? in the main class or in the arrayqueue class?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.