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
Mathclass 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
Create a driver class called
TestBookto test your
Bookclass that you wrote in the last practice exercise. In the main method of
TestBook, create a
Bookobject using the 5-argument constructor. Print out the result of calling all of the getter methods on the
Bookobject. 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
Bookobject 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 firstname.lastname@example.org for inquiries.