variable n is already defined in java

I am trying to dynamically create objects in java uses an array

int  number = Integer.parseInt(JOptionPane.showInputDialog("Enter number of objects"));
        int n[] = new int[number];
        for (int i = 0 ; i < number ; i++) {
          SomeClass n[i] = new SomeClass(1,5,6);

        }

However the line SomeClass n[i] = new SomeClass(1,5,6); throw the following error

variable n is already defined

Jon Skeet
people
quotationmark

Your code looks like it's trying to declare n[i] as a variable. You don't need to do that, because n is already declared. You just need an assignment to the array element:

n[i] = new SomeClass(1, 5, 6);

... but you'll also need to change the type of n:

SomeClass[] n = new SomeClass[number];

(You could use SomeClass n[] but that style of syntax is discouraged; it's clearer if you put all the type information in one place.)

people

See more on this question at Stackoverflow