1

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.

Community
  • 1
  • 1
  • *I* *am* *SO* *SORRY* for this, but I *MUST* make one grammar correction: "Then I runned #java DataStructure It runned fine." ==> "Then I ran #java DataStructure It ran fine.". The rest of the description is fine and coherent, but this is a big grammar glitch. – David Elson Jun 14 '15 at 00:37
  • A file name of the form `DataStructure$1.class` is what `javac` would use for an anonymous inner class. I don't see any in your code, so I *speculate* that `javac` is generating a synthetic one, but the other compilers are not. This may be related to your use of a private inner class class with only a default constructor. Try adding a public or default-access constructor to `DataStructure.EvenIterator`. It doesn't have to *do* anything -- it's just a way to set the access level for the nullary constructor. – John Bollinger Jun 14 '15 at 01:30
  • Out of interest, are you running the maven in eclipse. Are you using the embedded maven installation and which compiler are you using. I supect that the answers to these questions will point to using the eclipse compiler in maven and this is therefore a difference between the oracle and eclipse compilers. I would then echo what @John Bollinger said above. – redge Jun 14 '15 at 02:07
  • @DavidElson, I'm sorry for the mistake, but I apreciate the correction. English is not my native language and I'm continuously learning it. Thank you, anyway. – Italo Macellone Jun 14 '15 at 16:26
  • @JohnBollinger, Adding a public constructor with no args did the job. Thank you! – Italo Macellone Jun 14 '15 at 16:34
  • @redge, I'm on linux terminal. (java and javac 1.8.0_25. maven 3.0.5.) I tried commands such as mvn clean compile and javac. javac ended up with one extra file, whilst mvn didn't. Eclipse was my last trial that ended up just like maven. Thank you – Italo Macellone Jun 14 '15 at 16:41
  • He other possibility is the version of java language spec being used. Is your maven config specifyong a java version? If not it would be 1.5. Javac would use its default (8.0) maven may be using something else . Eclipse is probably taking its lead from maven. – redge Jun 15 '15 at 10:54
  • @redge, maven is also using jdk 1.8.0_25. – Italo Macellone Jun 16 '15 at 13:33

0 Answers0