0

I have an ArrayList of Objects. The Objects are flowers. After adding flowers to a list of flowers, I want to print out each flower's name. Whenever I add a flower, I give the flower a name. Say I add three flowers named "Rose", "Tulip", and "Daffodil". When I run my function that iterates through the ArrayList, all I get is three Daffodils. If I add five flowers, it displays five flowers but all of them have the name of the last flower I added. Here is my main class:

import java.util.Scanner;
import java.util.ArrayList;

public class mainClass {

    static ArrayList<flowerClass> flowerPack = new ArrayList<flowerClass>();


    public static void main(String[] args){
        Scanner input = new Scanner(System.in);

        while(true){
            System.out.println("1. Add flower to flowerpack.");
            System.out.println("2. Display the flowers in the flowerpack.");

            int userChoice = input.nextInt();

            switch(userChoice){
            case 1:
                addFlower();
                break;
            case 2:
                displayFlowers();
                break;
            }
        }
    }

    public static void addFlower(){
        Scanner input = new Scanner(System.in);
        System.out.println("What is the flower's name?");
        String flowerName = input.nextLine();
        flowerPack.add(new flowerClass(flowerName));
    }

    public static void displayFlowers(){

        for (Object o: flowerPack){
            System.out.println(flowerClass.getName());
        }
    }
}

Here is my flowerClass:

    public class flowerClass {

    public static int numberOfFlowers = 0;
    public static String flowerName = null;
    static String flowerColor = null;
    static int numberThorns = 0;
    static String flowerSmell = null;

    flowerClass(String desiredName){
        flowerName = desiredName;
        numberOfFlowers++;
    }

    public static void setName(String desiredName){
        flowerName = desiredName;
    }

    public static String getName(){
        return flowerName;
    }
}

I have tried using an array of objects instead of an ArrayList. I can get it to display the unique address of each separate flowerClass object but I can never successfully access each object's flowerName to display it to the user. Thanks for any suggestions.

HandleThatError
  • 598
  • 7
  • 29
  • 5
    remove `static` key word from everywhere in `flowerClass` – jmj Jun 20 '14 at 20:46
  • change your for loop to `for(flowerClass o : flowerPack)` and remove static from everywhere – A4L Jun 20 '14 at 20:49
  • 1
    possible duplicate of [In laymans terms, what does 'static' mean in Java?](http://stackoverflow.com/questions/2649213/in-laymans-terms-what-does-static-mean-in-java) – femtoRgon Jun 20 '14 at 20:49
  • Thanks a lot guys! I'll go back and review static, public, private. It seems that was the biggest headache, as well as replacing Object with flowerClass. I'll give all of this a go and review the keywords. – HandleThatError Jun 20 '14 at 20:56
  • The reason I don't feel this is a duplicate is because this question also deals with the proper way to iterate through an ArrayList of objects. It doesn't deal with just the static itself. And beyond accessing the methods directly against the class, my issue also dealt with nuances in creating multiple objects and accessing specific attributes rather than just a method. – HandleThatError Jun 20 '14 at 21:26

2 Answers2

2

A couple of things. First, you should not set this as static:

public static String flowerName = null;

As a static variable, all instances of flowerClass will have the same flowerName. It should be:

public String flowerName;  // null is implicit

Second, your loop:

for (Object o: flowerPack){
    System.out.println(flowerClass.getName());
}

What you are doing here is calling getName() on the class flowerClass, not an instance of flowerClass. They only reason this even compiles is because you marked those methods as static.

Remove static from your getter and setter (again you want to access the instance of flowerClass, not the class flowerClass):

public void setName(String desiredName){
    flowerName = desiredName;
}

public String getName(){
    return flowerName;
}

Then iterate over the instances of flowerClass:

for (flowerClass flower: flowerPack){
     System.out.println(flower.getName());
}
martinez314
  • 12,162
  • 5
  • 36
  • 63
0

This is because your setter and getter methods as well as the member variable 'name' are static. So whenever you set flower's name, you are basically overwriting the previous flower's name. Since there is only one name for all the objects. All your objects basically point to the same name.

Vivin
  • 1,327
  • 2
  • 9
  • 28