I am trying to learn Java.
I have a custom class, which has attributes:
public class Person{
private String name;
}
I have another class, where I do:
public class foo{
private Person guy;
public void setGuy(Person guy){
this.guy = guy;
}
public Person getGuy(){
return guy;
}
void someMethod(){
Person anotherGuy = new Person();
anotherGuy = getGuy();
}
}
I am confused when I use the getGuy() method.
I thought that when I do this:
Person anotherGuy = new Person();
anotherGuy = getGuy();
I create a new object, which has the same value as guy. But it seems that anotherGuy is actually a pointer to guy.
So
Person anotherGuy = getGuy();
and the above 2 lines, do the exact same thing? I am confused. Also then how do I create a entirely new object in memory?
No, the other version creates a new Person() object first and then discards it (which is just a waste of memory and processor cycles).