Java 17 Recipes
-2. Making Private Fields Accessible to Other
Download 3.2 Mb. Pdf ko'rish
|
Java 17 Recipes
- Bu sahifa navigatsiya:
- How It Works
5-2. Making Private Fields Accessible to Other
Classes Problem You want to create private instance members so that outside classes cannot access them directly. However, you would also like to make those private members accessible in a controlled manner. Solution Encapsulate the private fields by making getters and setters to access them. The following code demonstrates the declaration of a private field, followed by accessor (getter) and mutator (setter) methods that obtain or set the value of that field from an outside class. Chapter 5 ObjeCt-Oriented java 153 private String firstName; /** * @return the firstName */ public String getFirstName() { return firstName; } /** * @param firstName the firstName to set */ public void setFirstName(String firstName) { this.firstName = firstName; } The getFirstName() method can be used by an outside class to obtain the value of the firstName field. Likewise, an outside class can use the setFirstName(String firstName) method to set the value of the firstName field. How It Works Often when fields are marked as private within a class, they still need to be made accessible to outside classes to set or retrieve their value. Why not just work with the fields directly and make them public then? It is not good programming practice to work directly with fields of other classes because access can be granted in a controlled fashion by using accessors (getters) and mutators (setters). By not coding directly against members of another class, you also help decouple the code, which helps to ensure that if an object changes, others that depend on it are not adversely affected. As you can see from the example in the solution to this recipe, hiding fields and working with public methods to access those fields is fairly easy. Simply create two methods; one to obtain the value of the private field, the getter or accessor method. And the other to set the value of the private field, the setter or mutator method. In the solution to this recipe, the getter returns the unaltered value contained within the private field. Similarly, the setter sets the value of the private field by accepting an argument of the same data type as the private field and then setting the value of the private field to the value of the argument. Chapter 5 ObjeCt-Oriented java 154 The class using the getters or setters for access to the fields does not know any details behind the methods. For instance, a getter or setter method could contain more functionality if required. Furthermore, the details of these methods can be changed without altering any code that accesses them. Note Using getters and setters does not completely decouple code. in fact, many people argue that using getters and setters is not a good programming practice. Objects that use the accessor methods still need to know the type of instance field they are working against. that said, getters and setters are standard techniques for providing external access to private instance fields of an object. to make the use of accessor methods in a more object-oriented manner, declare them within interfaces and code against the interface rather than the object itself. Download 3.2 Mb. Do'stlaringiz bilan baham: |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling