Java 17 Recipes
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- 5-6. Defining an Interface for a Class
How It Works
The JavaBean is an object that holds information so that it can be passed around and used within an application. One of the most important aspects of a JavaBean is that its fields are declared as private. This prohibits other classes from accessing the fields directly. Instead, each field should be encapsulated by methods defined to make them accessible to other classes. These methods must adhere to the following naming conventions. • Methods used for accessing the field data should be named using a get prefix, followed by the field name. • Methods used for setting the field data should be named using a set prefix, followed by the field name. For instance, in the solution to this recipe, the Team object contains a field with players’ names. To access that field, a getPlayers method should be declared. It should return the data that is contained within the players field. Likewise, a setPlayers method should be declared to populate the players field. It should accept an argument of the same type as the players field, and it should set the value of the players field equal to the argument. This can be seen in the following code. public List getPlayers() { return players; } void setPlayers(List players) { this.players = players; } JavaBeans can populate lists of data written to a database record or for a myriad of other functions. Using JavaBeans makes code easier to read and maintain. It also helps to increase the likelihood of future code enhancements because very little code implementation is required. Another benefit of using JavaBeans is that most major IDEs autocomplete the encapsulation of the fields for you. Chapter 5 ObjeCt-Oriented java |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling