0

I'm writing the following code purely for fun and there are probably still errors or it may not even work at all:

public class PrimeGenerator implements PrimitiveIterator.OfInt {
    private final static IntNode HEAD_NODE = new IntNode(2); //error here

    private IntNode lastNode = HEAD_NODE;
    private int current = 0;

    @Override
    public boolean hasNext() {
        return true;
    }
    @Override
    public int nextInt() {
        if (lastNode.value == current) {
            lastNode = lastNode.next;
            return lastNode.value;
        }
        while (true) {
            current++;
            if (isPrime(current)) {
                break;
            }
        }
        appendNode(current);
        return current;
    }

    private boolean isPrime(final int number) {
        PrimeGenerator primeGenerator = new PrimeGenerator();
        int prime = 0;
        while (prime < number) {
            prime = primeGenerator.nextInt();
            if (number % prime == 0) {
                return false;
            }
        }
        return true;
    }

    private void appendNode(final int value) {
        couple(lastNode, new IntNode(value));
    }

    private void couple(final IntNode first, final IntNode second) {
        first.next = second;
        second.previous = first;
    } 

    private class IntNode {
        public final int value;

        public IntNode previous;
        public IntNode next;

        public IntNode(final int value) {
            this.value = value;
        }
    }
}

For people not known with Java 8, don't worry, PrimitiveIterator.OfInt, works the same as Iterator<Integer>.

The issue I am having is on the second line, namely the:

private final static IntNode HEAD_NODE = new IntNode(2);

I get the warning: non-static variable this cannot be referenced from a static class.

I have searched and it should be able to be fixed by making IntNode non-dependant on PrimeGenerator, by moving it in its own public class.

However, what can I do if I do not want IntNode to be publicly known?

2
  • Make it a static class? Commented Feb 23, 2014 at 12:32
  • @OliCharlesworth Doesn't that defend the idea of being able to instantiate it? Or does it work different with inner classes, I am still confused about that. Commented Feb 23, 2014 at 12:33

1 Answer 1

3

You should make your IntNode class static.

If you don't, this means that an IntNode instance cannot exist without an instance of the enclosing class already existing.

In short, you cannot write (provided IntNode was public of course):

new PrimeGenerator.IntNode(whatever);

but you'd have to create a new PrimeGenerator, say generator below, and write:

generator.new IntNode(whatever);

Here however you try and create an IntNode as a class variable for PrimeGenerator. This won't work, since you don't have an instance of PrimeGenerator yet.

Sign up to request clarification or add additional context in comments.

4 Comments

Is it safe to remember that an static inner class is the equivalent of a public class in its own file, with as only difference that the inner variant is never visible to the outside?
No, not quite; the way I remind it is that instances of a static inner class can exist independently of its outer class. Even "instance dependent" inner classes can be visible from the outside if they are declared public.
Also note that in your inner class you don't need to make the variables public, you can make them private: they will be equally visible from the inner class.
Except that Netbeans complains that the variable is not being used (even though it is by the outer class!), and I hate complaints from IDEs.

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.