Results 1 to 3 of 3
  1. #1
    rollei is offline Member
    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    Default Problem with Class String Variable

    Dear all, I'm writing some code, but I run into this problem
    I have this class Inventory and ingredient. Within Inventory, there is an ArrayList of ingredients. I also have a function addIngredient in Inventory that adds an ingredient to the ArrayList. My problem is, after adding several ingredients to the ArrayList, each of the names of the Ingredients are all the same.
    Java Code:
    import java.util.ArrayList;
    
    public class Inventory {
    	int numIngred;;
    	ArrayList ingred = new ArrayList<Ingredient>();
    	
    	public Inventory() {
    		numIngred = 0;
    	}
    
    	public int getNumIngred() {
    
    		return numIngred;
    	}
    	
    	public String getIngredientName(int n) {
    		return ((Ingredient)ingred.get(n)).getName();
    		
    	}
    	
    	public Ingredient getIngredient(int n) {
    		return (Ingredient)ingred.get(n);
    	}
    
    	public void addIngredient(String iname) {
    		numIngred++;
    		ingred.add(new Ingredient(iname));
    	}
    }
    
    
    import java.util.ArrayList;
    
    public class Ingredient {
    	public static String name;
    
    	
    	public Ingredient( String str ) {
    		name = str;
    	}
    	
    	public String getName() {
    		return name;
    	}
    	
    	public void setName(String s) {
    		name = s;
    		
    	}
    }
    
    //In some other class's main:
    Inventory inven = new Inventory();
    inven.addIngredient("Cheese");
    inven.addIngredient("Apples");
    inven.addIngredient("Carrots");
    inven.addIngredient("Peanuts");
    But, again, all the ingredient names end up being the last name added, "Peanuts". Am I missing a subtly in passing and setting Strings?

    Thank you,
    Alex

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    each of the names of the Ingredients are all the same.
    Sounds like you have one object with many references to it.
    Or you are using static variables.
    Last edited by Norm; 08-03-2011 at 01:27 PM.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,525
    Blog Entries
    7
    Rep Power
    20

    Default

    Why is your ŽnameŽ String in the Ingredient class static?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 5
    Last Post: 01-26-2011, 06:38 PM
  2. problem regarding giving value to string variable
    By enggvijaysingh@gmail.com in forum Advanced Java
    Replies: 3
    Last Post: 12-17-2010, 10:11 PM
  3. String variable problem
    By gkoef in forum New To Java
    Replies: 4
    Last Post: 12-05-2010, 02:15 PM
  4. Replies: 6
    Last Post: 03-31-2009, 04:27 AM
  5. Replies: 1
    Last Post: 08-03-2007, 10:52 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •