I've got the following code as an example(I've removed most of the code to make it easier)
Main class:
class TestStudent_3 { // To update student name to other name private void updateObject (Student s, String otherName) { s.setName (otherName); } public static void main (String [] args) { ... All variables, methods and scanner inputs etc removed ... Student s = new Student (name, gender, age, subject1, subject2); s.displayStudentInfo (); // If you wish to change one the fields, you need to use mutator methods TestStudent_3 ts = new TestStudent_3 (); ts.updateObject (s, "New Name"); s.displayStudentInfo (); } } Student class:
class Student { private String name; ... All variables, methods and scanner inputs etc removed ... public void setName (String name) { this.name = name; } } My question is what's these few lines in the main method doing? And why can it update the existing record('s')
TestStudent_3 ts = new TestStudent_3 (); What's this doing? Creating a new object ts?
ts.updateObject (s, "New Name"); Passing Student s object variables(contents) over to the updateObject method together with "New Name" string, why does this work?
Thanks in advance!