Quinma
Quinma

Reputation: 1476

Java class variables equal same value

I am preparing for a certification exam and I don't understand this code:

Main:

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);
    }
}

Class:

public class Student {
    public String name = "";
    public int age = 0;
    public String major = "Undeclared";
}

Why does this output "Bob's Name: Jian"?

Bob.name was never set to Jian. Obviously it must be because "jian = bob;" but i thought that would only set jian variables to the same as bob. What is this concept called and where is it explained in java tutorials?

Upvotes: 1

Views: 4042

Answers (6)

devrobf
devrobf

Reputation: 7213

This is a fundamental concept in Java: all object variables - that is, objects such as bob and jian which represent instances of Java Classes - are references. This concept is also referred to as pointers in languages such as C.

This means that your variable bob does not, conceptually, hold the values of the name, age and major fields, instead it points to a location in memory which holds the data. Accordingly, when you use an assignment operator (that's the = symbol) with object reference variables, you are simply changing which part of memory that variable points to. So the line:

bob = jian

means that the variable bob now refers to the same area of memory as jian, and anything you do in one will be reflected in the other.

This leaves an interested question of what has happened to the area of memory which holds Bob's data. Since we no longer have a reference to it, it is no longer possible for us to get hold of it, even though it still exists somewhere in memory (although Java will soon notice and clear it up - known as garbage collection).

Anyway, this sort of concept is better expressed diagrammatically, so I suggest looking at the official Java documentation or this tutorial.

Upvotes: 1

Martin Wilson
Martin Wilson

Reputation: 3386

The variables bob and jian are object references. See http://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html

This article contains an example similar to yours: http://www.informit.com/articles/article.aspx?p=174371&seqNum=4

Upvotes: 0

Chris Dodd
Chris Dodd

Reputation: 126378

That's because bob and jian (like all non-primitive variables in java) are references (pointers to objects) rather than objects. So when you say jian = bob;, that makes jian point at the same object as bob.

Upvotes: 1

Saurabh Sharma
Saurabh Sharma

Reputation: 2341

Look at the code `"jian = bob;" You changed the reference of the bob object and set it to jian.
Then setting jian.name = "Jian" Also changes the bob object

Upvotes: 2

Alex Stybaev
Alex Stybaev

Reputation: 4693

now your jian object reference points on the same memory part as object reference bob, so if you change one of them - it will affect both of them, cause now they are the same object.

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Assignments in Java do not copy objects, they copy references. After this assignment

jian = bob;

your jian is no longer pointing to the Student object that you have allocated and assigned to jian, it's the same as bob, creating an alias to the same object. The original jian is now irretrievably lost, becoming eligible for garbage collection.

The following assingment

    jian.name = "Jian";

overwrites the name in the bob variable through its jian alias, leading to the result that you see.

Upvotes: 3

Related Questions