0

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!

2
  • 1
    "why does this work" why shouldn't it? Commented Jan 22, 2020 at 16:32
  • I'm just curious as to why it works as I'm not how it's working. Why is a new object ts in the main class created and why does passing values of the previous object Student s and string of "New Name" allows the object Student s to be updated? Commented Jan 22, 2020 at 16:35

3 Answers 3

1

Break it down.

s.setName(otherName);

sets the Name in instance s to otherName

ts.updateObject (s, "New Name");

is acting like a proxy of sorts. It accepts an instance of s and the name and then does the same thing in the updateObject method.

Sign up to request clarification or add additional context in comments.

8 Comments

So it's actually "overwriting" the instance of s(in this case, the name variable) in the process? Not actually "searching and matching" instance s and replacing? ahhh it actually makes sense to overwrite, was confused as to how the statements are actually able to "search and match" the instance s earlier and replace the new variable only
It is overwritting s's name field, not the instance of s. It is just using s to make the change. No change is made to ts at all when updateObject is called. But it appears you do understand now.
So ts.updateObject (s, "New Name"); passes the instance s over to updateObject method so it can be used as s.setName to set the name right? ahh I see
What about the private void updateObject (Student s, String otherName)? Why does does it have to be Student s? Is it like telling the method, hey we have a instance 's' coming through?
@Michelle C s is just the variable name to be used inside the method, it doesn't need to match the name you pass it from main. Student is to signify that the method should expect a Student object in the first parameter when being called from somewhere (in this case the main).
|
1

It seems you are confused why the ts object had to be created with TestStudent_3 ts = new TestStudent_3 (); in order to update the Student s.

updateObject is not static, and is part of the TestStudent_3 class, so in order to call that method, you need to have an object of the class and invoke it using the object, as you see with the ts.updateObject(s, "New Name") where ts is the object created just so you can use the method.

Realistically updateObject should be static the way it is written:

private static void updateObject (Student s, String otherName)

Because the ts object is not used at all for logic of the method, it is completely unnecessary. If it was static, then you would just be able to use updateObject(s, "New Name") instead without the ts. (although you might as well just call s.setName("New Name") directly).

Comments

1

You pass an object reference to object s into the updateObject method.

So the updateObject method call:

<reference to s>.setName(otherName)

or you can say:

s.setName(otherName)

That will change the name value of the Student s object.

In the next line in the main method you call:

s.displayStudentInfo();

Thats kind of equal to:

<reference to s>.displayStudentInfo();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.