0

Here is my code bellow. This is any abstract class with instance array queue. I never assign array size anywhere. But I can still use the array and can iterate to add data to the array. How is this possible?

public abstract class Operations {
    int queue[];
    int head;
    int tail;

    public abstract String enqueue(int value);
    public abstract String dequeue();
    public abstract String peek();
    public abstract String delete();
    public abstract boolean isEmpty();
    public abstract boolean isFull();
    public abstract void display();
}
8
  • 1
    "> But I can still use the array and can iterate to add data to the array" : no you shouldn't be able to - it's a null field. Show some code of you using the class Commented Apr 17, 2020 at 7:20
  • 1
    Have you also ran the code? If the array is never initialised it is set to null and will throw a NullPointerException when used. This being an abstract class, can you also show an implementation of this class that is actually used? Commented Apr 17, 2020 at 7:22
  • This is another class that extends the abstract class paste.ubuntu.com/p/YBpzvHv2W7 Commented Apr 17, 2020 at 7:29
  • And this is my Main class though it isn't completed yet. I just working with some portion. paste.ubuntu.com/p/xDyRb6VwMw Commented Apr 17, 2020 at 7:36
  • @Asibul see my edited reply. Your code doesn't work, as expected Commented Apr 17, 2020 at 7:39

1 Answer 1

1

Simplified the variable queue just holds an address to the memory location of the beginning of the array or in this case null at the beginning

At some point in your code you assign a new array to it (otherwise you will get a NullPointerException) using new int[..] which will then allocate the required memory and assign it to the variable.

Update: I've executed OP's code and as expected get a NullPointerException when calling enqueue.

Choose option:
1. Enqueue
 2. Dequeue
 3. Peek
 4. Delete
 5. Exit
1
Enter a value to enqueue:
12
Exception in thread "main" java.lang.NullPointerException
    at test.Queue.isFull(Queue.java:16)
    at test.Queue.enqueue(Queue.java:50)
    at test.Test.main(Test.java:21)
Sign up to request clarification or add additional context in comments.

4 Comments

But my question is without assigning size of the array how I can use the array?
@Asibul You are basically asking "how can I use an array without having an array?" You can't.
@Asibul you can't. You can pass it around without looking at it, but the value being "stored" in that variable is null.
I know it's not possible. But my code is working. Can you please check my code once? Main class: paste.ubuntu.com/p/RjgkBPsHgR Abstract class: paste.ubuntu.com/p/2nS6wT4KZK Extended class: paste.ubuntu.com/p/YBpzvHv2W7

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.