Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student...
Transcript of Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student...
![Page 1: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/1.jpg)
Object Instantiation and Initialization in Java
CSCI 111
![Page 2: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/2.jpg)
Computer Memory Driver Java Code
What Happens?
![Page 3: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/3.jpg)
student1
Computer Memory
Student student1;
Driver Java Code
What Happens? A new variable, student1, is created. student1 can ONLY hold an instance of the Student class. i.e. student1 cannot hold a String (“Joe”) or an integer (6). This is called Variable Declaration.
![Page 4: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/4.jpg)
student1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2);
Driver Java Code
What Happens?
![Page 5: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/5.jpg)
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2);
Driver Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
![Page 6: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/6.jpg)
Student student1; student1 = new Student(“Joe”, 123, 3.2);
Driver Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
![Page 7: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/7.jpg)
public Student(String inName, int inID, double iG) { name = inName; idNum = inID; gpa = iG; }
Student Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
![Page 8: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/8.jpg)
public Student(String inName, int inID, double iG) { name = inName; idNum = inID; gpa = iG; }
Student Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
“Joe” 123 3.2
![Page 9: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/9.jpg)
public Student(String inName, int inID, double iG) { name = inName; idNum = inID; gpa = iG; }
Student Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
“Joe” 123 3.2 “Joe” 123
3.2
![Page 10: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/10.jpg)
public Student(String inName, int inID, double iG) { name = inName; idNum = inID; gpa = iG; }
Student Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
“Joe” 123 3.2 “Joe” 123
3.2
![Page 11: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/11.jpg)
Driver Java Code
What Happens? 1. A new Student object (instance of the Student
class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2);
![Page 12: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/12.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2);
1. A new Student object (instance of the Student class) is created in memory with the default instance variable values. This set is called Instantiation.
2. The constructor in the Student class is called to populate variables with their initial values. This step is called Initialization.
3. student1 is set to point to this new object. This step is called Variable Assignment.
![Page 13: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/13.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName());
Whose getName() method are we calling?
![Page 14: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/14.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName());
Whose getName() method are we calling?
The object that student1 is pointing to.
![Page 15: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/15.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName());
Whose getName() method are we calling?
The object that student1 is pointing to.
So go to the object that student1 is pointing to and look at the getName() method.
![Page 16: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/16.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public String getName() { return name; }
Whose getName() method are we calling?
The object that student1 is pointing to.
So go to the object that student1 is pointing to and look at the getName() method.
![Page 17: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/17.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public String getName() { return name; }
Whose getName() method are we calling?
The object that student1 is pointing to.
So go to the object that student1 is pointing to and look at the getName() method.
![Page 18: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/18.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public String getName() { return name; }
“Joe”
Whose getName() method are we calling?
The object that student1 is pointing to.
So go to the object that student1 is pointing to and look at the getName() method.
The value “Joe” is returned to whoever called the method (the Driver).
![Page 19: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/19.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Whose getName() method are we calling?
The object that student1 is pointing to.
So go to the object that student1 is pointing to and look at the getName() method.
The value “Joe” is returned to whoever called the method (the Driver).
Joe is printed.
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); “Joe”
![Page 20: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/20.jpg)
Driver Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”);
![Page 21: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/21.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
public void changeName(String newName) { name = newName; }
![Page 22: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/22.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public void changeName(String newName) { name = newName; }
“Joseph”
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
newName contains the value “Joseph”.
![Page 23: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/23.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public void changeName(String newName) { name = newName; }
“Joseph”
“Joseph”
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
newName contains the value “Joseph”.
![Page 24: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/24.jpg)
Student Java Code
What Happens?
name: “Joe” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
public void changeName(String newName) { name = newName; }
“Joseph”
“Joseph”
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
newName contains the value “Joseph”.
![Page 25: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/25.jpg)
Student Java Code
What Happens?
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
Whose changeName(parameter) method are we calling? The object student1 is pointing to. So go to the object student1 is pointing to and look at the changeName(parameter) method.
newName contains the value “Joseph”.
The variable name, in the object student1 is pointing to, is changed to “Joseph”.
public void changeName(String newName) { name = newName; }
“Joseph”
“Joseph”
![Page 26: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/26.jpg)
Driver Java Code
What Happens?
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
Computer Memory
The getName() method is called on the object that student1 is pointing to. Thus, the current value that is in the name variable is returned to whoever asked (the Driver). The Driver then prints this value which is “Joseph”.
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName());
![Page 27: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/27.jpg)
Driver Java Code
What Happens?
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName()); Student student2 = new Student(“Sally”, 321, 3.7);
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
student2
Computer Memory
1. Variable Declaration.
![Page 28: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/28.jpg)
Driver Java Code
What Happens?
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName()); Student student2 = new Student(“Sally”, 321, 3.7);
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent2
Computer Memory
1. Variable Declaration.
2. Object Instantiation.
![Page 29: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/29.jpg)
Driver Java Code
What Happens?
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName()); Student student2 = new Student(“Sally”, 321, 3.7);
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
name: “Sally” idNum: 321 gpa: 3.7
getName() … changeName(…) s
tudent2
Computer Memory
1. Variable Declaration.
2. Object Instantiation.
3. Object Initialization.
![Page 30: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/30.jpg)
Driver Java Code
What Happens?
1. Variable Declaration.
2. Object Instantiation.
3. Object Initialization.
4. Variable Assignment.
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName()); Student student2 = new Student(“Sally”, 321, 3.7);
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
name: “Sally” idNum: 321 gpa: 3.7
getName() … changeName(…) s
tudent2
Computer Memory
![Page 31: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/31.jpg)
Driver Java Code
What Happens?
Student student1; student1 = new Student(“Joe”, 123, 3.2); System.out.println(student1.getName()); student1.changeName(“Joseph”); System.out.println(student1.getName()); Student student2 = new Student(“Sally”, 321, 3.7);
name: “Joseph” idNum: 123 gpa: 3.2
getName() … changeName(…) s
tudent1
name: “Sally” idNum: 321 gpa: 3.7
getName() … changeName(…) s
tudent2
Computer Memory
![Page 32: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/32.jpg)
Exercises
![Page 33: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/33.jpg)
Driver Java Code
Student Java Code
Student student1 = new Student(“Joe”, 123, 3.2);
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
public Student(String inName, int inID, double iG) { name = inName; idNum = idNum; gpa = gpa; }
![Page 34: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/34.jpg)
Driver Java Code
Student Java Code
Student student1 = new Student(“Joe”, 123, 3.2);
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
public Student(String inName, int inID, double iG) { inName = name; idNum = inID; gpa = iG; }
![Page 35: Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student object (instance of the Student class) is created in memory with the default instance](https://reader034.fdocuments.net/reader034/viewer/2022050303/5f6c4de9f72802687232aba1/html5/thumbnails/35.jpg)
Driver Java Code
Student Java Code
Student student1 = new Student(“Joe”, 123, 3.2);
name: null idNum: 0 gpa: 0
getName() … changeName(…) s
tudent1
Computer Memory
public Student(String name, int idNum, double gpa) { name = name; idNum = idNum; gpa = gpa; }
A little trickier. Remember that when Java sees the variable “name” inside the constructor, it will consider the variable “name” defined there, if there is one and not the instance variable “name”.