0

I am trying to understand something that may be very simple, so I apologize if I am looking like a rookie.

I have this java class:

public class Student {
    public String name = "";
    public int age = 0;
    public String major = "Undeclared";
    public boolean fulltime = true;

    public void display() {
        System.out.println("Name: " + name + " Major: " + major);
    }

    public boolean isFullTime() {
        return fulltime;
    }
}

And this class from which I call the first one:

public class TestStudent {
    public static void main(String[] args) {
        Student bob = new Student();
        Student jian = new Student();
        bob.name = "Bob";
        bob.age = 19;
        jian = bob;
        jian.name = "Jian";
        System.out.println("Bob s Name: " + bob.name);
    }
}

This will print:

Bob s Name: Jian

I am curious about how the assignments are done there.

2
  • Cause bob and jian is referring to the same object. Commented Dec 19, 2013 at 13:36
  • Thank you all for the answers, i upvoted all of them, because they were right. I chose @AnthonyGrist's answer because it was more clarifying. Thanks again! Commented Dec 19, 2013 at 13:51

7 Answers 7

3

This line causes jian and bob to point to the same object:

jian = bob;

So changing jian name will also change bob name.

jian.name = "Jian";
Sign up to request clarification or add additional context in comments.

Comments

2

When you do Student bob = new Student() you're creating an instance of the Student class, and saving a reference to that location in memory in the bob variable.

When you do jian = bob, you're saving that same reference to the memory location in the jian variable, so now jian and bob point to exactly the same instance of the Student class.

Comments

2
Student bob = new Student();
Student jian = new Student();
// you've created two objects and two references 
bob.name = "Bob"; // bob's name is now Bob
bob.age = 19; // bob's age is now 19
jian = bob; // the jian reference is now referring to the same object as bob
jian.name = "Jian"; // jian and bob are now referring to an object whose name is changed to Jian
System.out.println("Bob s Name: " + bob.name);

Comments

2

Jian has reference to bob object.

Comments

2

you are copying the reference of bob to jian.

now both are pointing the same object.

Comments

2

Assignement it's due to the line

jian = bob;

With this line, you replace the instance of Jian's Object by Bob's Object. That's mean Jian instance become Bob instance, but Bob instance keep existing.

So when you change the name of Jian, internally you change the name of Bob

Comments

1

As writed before, your variables bob and jian is link to the same object in memory. That was, for not primitives types when you use = operator you copy link. If you need create anoter object that will contains fields with previously entered data you need implement clone method. Or you can add constructor that get your object in parametrs and copy your field. Example:

jian = bob.clone();

About this on wikipedia

By constructor:

public Student(Strudent obj){
    this.name = obj.name;
    this.age = obj.age;
}

In main: Student jian = new Student(bob);

And you can find many variants of solving you problems. Use any variant that you like.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.