So i first declare this object of a class:
static enterprise[] en = new enterprise[10];
Then, inside main:
for(int i=0;i<=9;i++){
System.out.println("Insert name of the " + (i+1) + "ª enterprise");
en[i] = new enterprise(i);
Scanner scanner = new Scanner(System.in);
en[i].setName(scanner.next());
System.out.println(en[i].Name);
}
And then, in another method of the same class:
for(int i = 0; i<=9;i++){
System.out.println(en[i].index + "- " + en[i].Name);
}
So if at first I inserted on the first enterprise A, second B, C,D,E,F,G,H,I,J.. I should get as an output 1 A 2 B etc, but I get 9 J ten times. why does this happen?
Edit: here is the enterprise class: http://pastebin.com/gUCWRRgK
staticfields are shared among instances, so remove thestaticmodifier from those fields you want to have independent values per instance. See here, the relevant part of the Sun Java Tutorials.