Book Image

Java 9 with JShell

By : Gaston C. Hillar
Book Image

Java 9 with JShell

By: Gaston C. Hillar

Overview of this book

The release of Java 9 has brought many subtle and not-so-subtle changes to the way in which Java programmers approach their code. The most important ones are definitely the availability of a REPL, known as JShell, which will make experiments and prototyping much more straightforward than the old IDE-based project-led approach. Another, more subtle change can be seen in the module system, which will lead to more modularized, maintainable code. The techniques to take full advantage of object-oriented code, functional programming and the new modularity features in Java 9 form the main subjects of this book. Each chapter will add to the full picture of Java 9 programming starting out with classes and instances and ending with generics and modularity in Java.
Table of Contents (23 chapters)
Java 9 with JShell
Credits
About the Author
Acknowledgement
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Working with setters and getters


So far, we have been working with fields to encapsulate data in our instances. We could access the fields without any kind of restrictions as member variables for an instance. However, as it happens sometimes in real-world situations, restrictions are necessary to avoid serious problems. Sometimes, we want to restrict access or transform specific fields into read-only fields. We can combine the access restrictions to an underlying field with methods known as setters and getters.

Setters are methods that allow us to control how values are set; that is, these methods are used to change the values of related fields. Getters allow us to control the values that we return when we want to retrieve the value for a related field. Getters don't change the values of related fields.

Note

While some frameworks such as JavaBeans force you to work with setters and getters for each related field to be accessible, in other cases, setters and getters won't be necessary. In the...