0

I am trying to create a method that contains a for loop that creates new objects, but I am stuck at how to assign a predetermined name to an object based on the loop's count. For instance it would be something like this:

private void createPictureObject(int count){
  for(int a = 1; a <= count; a++){
  Picture picture*a* = new Picture(arguments);
}

The result of this loop would be that I have objects named something like picture1, picture2, picture3, picture 4 etc. Is this even possible?

Erran Morad
  • 4,563
  • 10
  • 43
  • 72
EG3542
  • 9
  • 1
  • This may work in other programming languages, but is not the *Way of Java* ™. Better to use an array, or a List or a Map to create collections of objects. Also please understand that variable names aren't nearly as important as one might think and almost don't exist in compiled code. Much better to concentrate on object **references** as that way leads towards truth. – Hovercraft Full Of Eels Jul 13 '14 at 05:55
  • Question has been duplicate-slammed. Shoot this same exact question gets asked about 3-5 times a week, and I'm not sure that we really need one more. – Hovercraft Full Of Eels Jul 13 '14 at 06:01

1 Answers1

2

No You cannot do that! Dynamic variable naming is not allowed in Java. Use an array instead.

Picture[] pictures = new Picture[10];
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289