1

What is happening when this code declares course2 = course1; Because what happens after that I can't make sense of. If I comment out course2 = course1; I get Programming Fundamentals twice.


public Course(int courseID, String courseName) {
    ID = courseID;
    name = courseName;
}

public String getName() {
    return name;
}

public void setName(String newName) {
    name = newName;
}

public static void main(String[] args) {
    Course course1 = new Course(2531, "Programming Fundamentals");
    Course course2 = new Course(1285, "Algorithms and Analysis");

    System.out.println(course1.getName());

    course2 = course1;
    course2.setName("PF");
    System.out.println(course1.getName());
}

This outputs as:
Programming Fundamentals
PF

N Woods
  • 37
  • 1
  • 6
  • I know this should be obvious, but I don't understand the logic here. – N Woods May 06 '18 at 08:13
  • It sets the references equal. What part of this confuses you exactly? – Boris the Spider May 06 '18 at 08:15
  • Here `course2` is pointing to same memory location as `course1` after eavluating `course2 = course1` so changing value by either `course1` or `course2` will reflect same memory location. This is Java 101 concepts. – Rajmani Arya May 06 '18 at 08:15
  • Thanks for pointing me in the right direction Boris... Why does it confuse me, well, we get to polymorphism and I'm thinking: I must have missed something basic here, not to mention it was in the mid-semester exam. It was taught back in week two, the tutor used rectangles to explain it, but her notes are rubbish and her lectures worse; it's confusing. So, I was simply trying to make it perfectly clear. – N Woods May 06 '18 at 08:43

3 Answers3

2
course2 = course1;

changes the value of course2 to be a reference to the same object referenced by course1 (the object created by the statement new Course(2531, "Programming Fundamentals")).

Therefore both course1 and course2 now reference the same object, so course2.setName("PF") has the same behavior as course1.setName("PF"). Hence the second System.out.println(course1.getName()) prints "PF".

If you comment out the course2 = course1; assignment, course2 references a different object, so course2.setName("PF") doesn't affect the object referenced by course1. Hence the second System.out.println(course1.getName()) still prints "Programming Fundamentals".

Eran
  • 387,369
  • 54
  • 702
  • 768
1

course1 is a variable, it reference an object, let's say object1.

course2 is another variable, it reference another object, let's say object2.

When you call course2 = course1, you make them reference the same object --object1.

xingbin
  • 27,410
  • 9
  • 53
  • 103
  • Thanks, nice and simple. I didn't need a complex answer. – N Woods May 06 '18 at 08:44
  • @NWoods See this about [what is an object](https://docs.oracle.com/javase/tutorial/java/concepts/object.html) and [what is variable](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html) – xingbin May 06 '18 at 08:48
0

course1 and course2 are references to objects of the class Course.
With course2 = course1 you change the object course2 points to to the same one as course1. So you access the same object after that with both course1and course2.

Turamarth
  • 2,282
  • 4
  • 25
  • 31