<< Chapter < Page | Chapter >> Page > |
C. 10
A setter and a getter
This is a very simple program that uses a setter (modifier or mutator) method named setX to set the value 10 in a property named x that is stored in an instance variable named y in an object instantiated from the class named NewClass ..
The program also uses a getter (accessor) method named getX to get and display the value of the property named x . (Note that according to JavaBeans design patterns, the name of the property isunrelated to the name of variable in which the property value is stored.)
D. 5
Hide your data and expose your methods
For reasons that I won't go into here, good object-oriented design principles state that in almost all cases where an instance variable is not declared to be final , it should be declared private . (A final variable behaves like a constant.)
What is private access?
When an instance variable is declared private , it is accessible only by methods of the class in which it is defined. Therefore, the only way that the"outside world" can gain access to a private instance variable is by going through an (usually public ) instance method of the object.
Accessor, modifier, mutator, setter, and getter methods
Historically, methods that have been defined for the purpose of exposing private instance variables to the outside world have been referred to as accessor and modifier methods. (Modifier methods are also sometimes called mutator methods.)
(Note that since the advent of Sun's JavaBeans Component design patterns, these methods have also come to be known as getter methods and setter methods indeference to the design-pattern naming conventions for the methods.)
A private instance variable with an initializer
The class named NewClass declares a private instance variable named x and initializes its value to 2, as shown in the following code fragment:
private int x = 2;
Two constructors
The class contains both a noarg constructor and a parameterized constructor as shown in the following fragment:
public NewClass(){
}//end constructorpublic NewClass(int x){this.x = x;
}//end constructor
Calling the noarg constructor
If an object of the class is instantiated by calling the noarg constructor, the initial value of 2 remains intact, and that object contains an instancevariable with an initial value of 2.
Calling the parameterized constructor
If an object of the class is instantiated by calling the parameterized constructor, the initial value of 2 is overwritten by the value of the incomingparameter to the parameterized constructor. In this case, that value is 5, because the object is instantiated by the following code fragment that passesthe literal value 5 to the parameterized constructor. Thus, the initial value of the instance variable in that object is 5.
NewClass obj = new NewClass(5);
A getter method
Because the instance variable named x is private , it cannot be accessed directly for display by the code in the makeObj method of the Worker class. However, the NewClass class provides the following public getter or accessor method that can be used to get the value stored in the instance variable.
Notification Switch
Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?