I have an object made in my main Recipe recipeOne = new Recipe("Pepperoni Pizza");
This object is an instance of this array of objects, defined and built here!
public class Recipe implements Cloneable{ String Name; final int INGREDIENT_ARRAY_MAX = 10; Ingredient Recipe[] = new Ingredient[INGREDIENT_ARRAY_MAX]; public Recipe(String name){ Name = name; }
So, I want to make a deep copy of this object with the line Recipe ressippi = (Recipe) recipe.clone(); and he will send me here!
public Object clone(){ Recipe cloneRec = new Recipe(Name); return cloneRec; }
I know that this is currently a shallow copy, because the method passes only links, so if I tried to change the name on my new object, which was a clone of recipeOne ... it would change both of their names. Obviously, I do not want this, I am pretty lost on this, can anyone help?
EDIT: @Rohit Jain
Both my Recipe class and my Ingredient class (objects that are stored in an array of recipes) have toString methods and ingredient recipes to print everything in a nice small format. When I call it my recipeOne object (the one called pepperoni pizza), I get Pepperoni Pizza: 1.0 pounds of dough, 8.0 ounces of sauce, 10.0 ounces of cheese
Then I go on to make a ressippi object and set it to the recipeOne clone, so everything is fine from here ... Then I change the name of ressippi to โPineapple Pizzaโ and it prints fine, but it does not print 3 ingredient objects that recipeOne kept what it should do!
source share