Execute the main method in personrunner


Discuss the below:

Q: Using the Person and PersonRunner classes below, execute the main method in PersonRunner. Why does the program end abnormally? Comment out the first two lines and run the program again. Why does the reference to p3 cause Frank's name to be printed when we set Frank's name with the p2 reference?

public class PersonRunner
{
public static void main(String[] args)
{
Person p1 = null;
p1.speak();
Person p2 = new Person("Beth");
p2.speak();
Person p3 = p2;
p2.speak();
p2.setName("Frank");
p3.speak();
}
}

public class Person
{
private String name;

public Person(String name)
{
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public void speak()
{
System.out.println("My name is " + name);
}
}

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Execute the main method in personrunner
Reference No:- TGS01934276

Now Priced at $20 (50% Discount)

Recommended (94%)

Rated (4.6/5)