$30 off During Our Annual Pro Sale. View Details »

CSE110 Lecture 20

CSE110 Lecture 20

Principles of Programming with Java
null and this
(202006)

Javier Gonzalez-Sanchez
PRO

June 16, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

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

    View Slide

  2. The toString Method

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  6. Previously
    Getters and Setters

    View Slide

  7. 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;
    }
    }

    View Slide

  8. 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;
    }
    }

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  12. The null reference

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  16. The this keyword

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide