5️⃣

12.5 Dot Notation

To use fields or methods that belong to a class (other than its constructors), you need to use dot notation. This is actually not anything new with this concept—you’ve used it to access methods in the Math class and to call methods on Strings. For example:

System.out.println(Math.PI); // dot notation to access the pi constant

String s = "abcd";
s = s.toUpperCase(); // dot notation to call toUpperCase method
System.out.print(s); // prints "ABCD"

Person myPerson = new Person(); // call constructor
myPerson.eat("berries"); // dot notation to accesss the eat method
View this code on GitHub

Practice

TestBook

Create a driver class called TestBook to test your Book class that you wrote in the last practice exercise. In the main method of TestBook, create a Book object using the 5-argument constructor. Print out the result of calling all of the getter methods on the Book object. Then use the setter methods to change each of the 5 fields, and print out the result of calling all of the getter methods on the Book object again to check that the setter methods work.

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.