Skip to content

Instantly share code, notes, and snippets.

@heiybb
Created April 20, 2018 00:14
Show Gist options
  • Save heiybb/1d69589f38f4fd3ddafbca2731580f9b to your computer and use it in GitHub Desktop.
Save heiybb/1d69589f38f4fd3ddafbca2731580f9b to your computer and use it in GitHub Desktop.
public class Course {
private String id;
private String name;
private Course(String ID, String NAME) {
id = ID;
name = NAME;
}
public static void main(String[] args) {
Course course1 = new Course("COS01", "ProgrammingF");
Course course2 = new Course("COS02", "ITIS");
// output course1 course2 name
System.out.println(course1.name);
System.out.println(course2.name);
System.out.println("########");
course2 = course1;
// re-set course2's name
course2.setName("PF");
// output course1 name
System.out.println(course1.name);
// course1 course 2 "identity hash code"
System.out.println(course1);
System.out.println(course2);
// actually we can see that both course1 and course2 are having the same address
// once the course2 = course1 is executed
}
private void setName(String newName) {
name = newName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment