If you were to create an object in a loop would it keep recreating that loop each iteration?

Viewed 68
while(!input.equals("-1")){

    plantName = "";
    plantCost = "";
    colorOfFlowers = "";
    isAnnual = false;

    if (input.charAt(0) == 'p') {
        Plant plant = new Plant();
        String[] temp = input.split(" ");
        plantName = temp[1];
        plantCost = temp[2];
        plant.setPlantName(plantName);
        plant.setPlantCost(plantCost);
        myGarden.add(plant);

     }else {
         Flower flower = new Flower();
         String[] temp = input.split(" ");
         flower.setPlantName(temp[1]);
         flower.setPlantCost(temp[2]);

        flower.setPlantType(Boolean.parseBoolean(temp[3]));
         flower.setColorOfFlowers(temp[4]);
         myGarden.add(flower);

     }
     input = scnr.nextLine();
 }

Here's an example of what I mean. at the beginning of the if statement I created a plant object and it ended up making the program work how I wanted it to, I'm just not exactly sure why it worked. What Im thinking is that by creating the object inside of the while loop it "deletes" at the end of the loop and creates a new one.

Again though I'm not sure because everytime I ask my professor a question about any concept he tells me to come here and ask someone. Thanks in advance.

2 Answers

You are saving a copy so the old object is getting written over.

Object[] a = new Object[10];

for (int i = 0; i < 10; i++) {
    // ob gets copied over with the new object
    Object ob = new Object();
    // but it is saved here
    a[i] = ob;
}

It's no different that this.

int[] n = new int[10];
for (int i = 0; i < 10; j++) {
    int x = i*10;
    n[i] = x; // x is saved in array.
}

When ever you call new Plant() You create a new instance of the Plant object. If you do not store it anywhere, it will get deleted by the java garbage collector once it leaves scope.

What is scope? It is basically inside your curly brackets of a loop, method if statement etc:

if( test == 5){
    Object instance1 = new Object();
    while(otherCondition == true){
        Object instance2 = new Object(); 
        // Now both instance1 and instance2 are in this scope 
    }
    // Now instance2 does is not longer in scope, and will be destroyed unless we store it somewhere.
}
//Now we do not have access to either instance1 and instance2 since they are both out of scope

When Objects leave the scope, thats a sign to java that we are done with them, and it should free them from memory, unless we tell java to store it somewhere.

ArrayList<Object> list = new ArrayList<Object>();
if( test == 5){
    Object instance1 = new Object();
    list.add(instance1);
    while(otherCondition == true){
        Object instance2 = new Object(); 
        list.add(instance2);
    }

}

In this second example, because we are adding the objects to the list, they are being stored, and therefore they won't be deleted.

In the case you have, it is very similar to the cases above, but instead of an if statement it is a loop. With loops every new iteration is sort of like its own scope, and because you are storing your Flower objects somewhere(with myGarden.add(flower);), they are not being deleted, but every iteration of the loop creates a new flower object.

Related