So I have 5 objects that I want to instantiate them using a for-loop
SomeObject obj1, obj2, obj3, obj4, obj5;
//Instead of having to write this
obj1 = new SomeObject();
obj2 = new SomeObject();
obj3 = new SomeObject();
//I want to be able to do this
for (int i = 0; i < 5; i++) {
//instantiate the objects with "new" keyword here
}
I have tried using an array, but they only create instances of objects inside the array only, and the original obj1,... didn't change at all
What I tried:
SomeObject obj1, obj2, obj3, obj4, obj5;
SomeObject[] objectArray = {obj1, obj2, obj3, obj4, obj5} //IntelliJ gives warning "Value obj1,... is always null"
for (int i = 0; i < 5; i++) {
objectArray[i] = new SomeObject(someRandomValueInsideHere);
}
System.out.println(obj1); //obj1 is still null, even though the loop instantiated the objects
System.out.println(objectArray[0]); //this exists. The whole array is also correctly populated
I read somewhere else about java objects being "passing reference by value". Can someone further explain that? Am I missing something really obvious here?