How To Draw Multiple Objects In A For Loop
posted 8 years ago
In Java, not-primitive variables hold references. When you practise
what you are doing is creating a new Horse object and putting a reference to that object within the horse variable. Now, if you did this
The 2nd line copies the reference.. not the object that it refers to. There is simply one Horse object
Now.. and array is a special kind of Object. Information technology's an object that holds multiple references. Once again,, it holds references, non objects..
Then, when you practice this
You have basically created an array object that tin can hold references to 10 horses. The 10 horses don't exist notwithstanding. The array is empty. Also think that the variable stable itself contains a reference to the array object. Everything is a reference.
Doing this
This creates an arrray object that can concord references to 10 horses and puts a reference to that array in the stable variable. Then it creates i horse and puts a reference into the horse1 variable. Then it copies the reference to the first element in the array. And so, not, the array that stable referes to contains a reference to 1 equus caballus and 9 other references are empty
You lot could shorten the above lawmaking to
The end event is the same. Y'all are just taking a shortcut and putting the reference to the Horse object direct into the array object
Now, to fill up all the x horsies in the stable.. y'all could practice this
Or do this
Both are equivalent equally far as how they execute. Even so, the 2nd version is more flexible.. Because you can easily increment the size of the stable similar this
>
Source: https://www.coderanch.com/t/628589/java/create-objects-loop
Posted by: hickstherinchis.blogspot.com

0 Response to "How To Draw Multiple Objects In A For Loop"
Post a Comment