Hallo, I'm very new to Java and OOP, so maybe my problem is a stupid one. Her it is:
I have a class "Person", with name and id. In the main programm I have an array of names done like this:
Now I would like to loop my array and create for each name a new istance of the class Person, something like this:
But this code of corse wouldn't work... because it would overwrite the p1 instance everytime. Do you know how to do it properly?
Thanks for suggestions.
I have a class "Person", with name and id. In the main programm I have an array of names done like this:
Code:
String[] Names =
{
"Pisellonio",
"Gianfranco",
"Euripide",
"Sofocle",
"Agamennone",
"Tafano",
"Asdrubale",
"Ippolito",
"Zaccaria",
"Lucrezia"
};
Now I would like to loop my array and create for each name a new istance of the class Person, something like this:
Code:
for (int i=0;i<Names.length;i++)
{
Person p1;
p1 = new Person();
p1.name = Names[i];
}
But this code of corse wouldn't work... because it would overwrite the p1 instance everytime. Do you know how to do it properly?
Thanks for suggestions.