+ All Categories
Home > Documents > Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student...

Object Instantiation and Initialization in Java · Driver Java Code What Happens? 1. A new Student...

Date post: 24-Jul-2020
Category:
Upload: others
View: 1 times
Download: 0 times
Share this document with a friend
35
Object Instantiation and Initialization in Java CSCI 111
Transcript
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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”.


Recommended