Would this Java code result in memory leak
class Employee {
Person person;
Employee() {}
String getPersonGender(String name) {
this.person = new Person(name);
return person.getGender().toString();
}
}
public static void main(String... args) {
Employee e;
String gender = e.getPersonGender("James"); // First Time
String gender = e.getPersonGender("Merry"); // Second Time
}
When from the main, we call getPersonGender for the 2nd time, would the Person object created the first time be available for garbage collected to free up?
Or would both Person object would be freed up only when we exit the main function?