Maven doesn't generate $1.class file. Javac does. Why?

286 views Asked by At

I was trying to test the following implementation on my own computer:

public class DataStructure {
    private final static int SIZE = 15;
    private int[] arrayOfInts = new int[SIZE];
    public DataStructure() {
        for (int i = 0; i < SIZE; i++) {
            arrayOfInts[i] = i;
        }
    }

    public void printEven() {
        DataStructureIterator iterator = this.new EvenIterator();
        while (iterator.hasNext()) {
            System.out.print(iterator.next() + " ");
        }
        System.out.println();
    }

    interface DataStructureIterator extends java.util.Iterator<Integer> { } 

    private class EvenIterator implements DataStructureIterator {
        private int nextIndex = 0;

        public boolean hasNext() {
            return (nextIndex <= SIZE - 1);
        }        

        public Integer next() {
            Integer retValue = Integer.valueOf(arrayOfInts[nextIndex]);
            nextIndex += 2;
            return retValue;
        }
    }

    public static void main(String s[]) {
        DataStructure ds = new DataStructure();
        ds.printEven();
    }
}

When I compiled the file with javac, it generated 4 files, which are:

DataStructure$DataStructureIterator.class,
DataStructure$EvenIterator.class,
DataStructure$1.class,
DataStructure.class

Then I ran

#java DataStructure

It ran fine.

What's bothering me is the fact of if I compile it using Eclipse or even maven, it won't generate 4 files, but 3, which are:

DataStructure$DataStructureIterator.class,
DataStructure$EvenIterator.class,
DataStructure.class

Now I'm wondering why this happens, although it runs fine as well.

Thank you. Ps: I couldn't comment on this existing post as I'm new to this: Why does Java code with an inner class generates a third SomeClass$1.class file?.

Sorry for being repetitive, but I think this is somehow tricky and interesting.

0

There are 0 answers