Slide 1

Slide 1 text

CSE110 Principles of Programming with Java Lecture 20: toString method, null and this Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com Office Hours: By appointment

Slide 2

Slide 2 text

The toString Method

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3 The toString Method • The toString method is a method that takes no parameter and returns a string • A returned string usually contains information on instance variables of its class. • Each class has a default toString method that contains its class object name and hash number. • When an object is used with System.out.println method, its toString method will be called.

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4 Example

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5 Example

Slide 6

Slide 6 text

Previously Getters and Setters

Slide 7

Slide 7 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7 Getters and Setters public class Example { private int number; //Getter or Accessor method public int getNumber() { return number; } //Setter or Mutator method public void setNumber(int numberFromOutside){ number = numberFromOutside; } }

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8 Getters and Setters public class Example { private int number; //Getter or Accessor method public int getNumber() { return number; } //Setter or Mutator method public void setNumber(int numberFromOutside){ number = numberFromOutside; } }

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9 Getters and Setters Do this: example.setNumber(1); System.out.println( example.getNumber() );

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10 Example

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11 Example

Slide 12

Slide 12 text

The null reference

Slide 13

Slide 13 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13 null • The null reference – the reference variable that does not currently point to an object. • Any variable of a class that was not instantiated/created using its constructor is a null. • If a program tries to access methods or variables, then you will get NullPointerException error during the execution of the program. In that case, check your program to see if it called a constructor to instantiate it.

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14 Example

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15 Example

Slide 16

Slide 16 text

The this keyword

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 17 this • The this reference refers to its class itself and can be used to refer to the instance variables of an object of the class. • You can create an instance variable and a local variable using a same name (compiles), but it is confusing! • In such case, local variables will be used, and instance variables will be ignored. We can use the this reference to refer to instance variables to distinguish them.

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 18 Example

Slide 19

Slide 19 text

CSE110 - Principles of Programming Javier Gonzalez-Sanchez [email protected] Summer 2020 Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.