Spelling/grammar fixes and some clarification (#23518)
* Spelling/grammar fixes and some clarification * Fixed the 'number' variable in the text
This commit is contained in:
@ -28,7 +28,7 @@ The setter method takes a parameter and assigns it to the attribute.
|
|||||||
|
|
||||||
Once the getter and setter have been defined, we use it in our main:
|
Once the getter and setter have been defined, we use it in our main:
|
||||||
```java
|
```java
|
||||||
public stativ void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Vehicle v1 = new Vehicle();
|
Vehicle v1 = new Vehicle();
|
||||||
v1.setColor("Red");
|
v1.setColor("Red");
|
||||||
System.out.println(v1.getColor());
|
System.out.println(v1.getColor());
|
||||||
@ -42,7 +42,8 @@ Getters and setters allow control over the values. You may validate the given v
|
|||||||
|
|
||||||
## Why getter and setter?
|
## Why getter and setter?
|
||||||
|
|
||||||
By using getter and setter, the programmer can control how important variables are accessed and updated, such as changing value of a variable within a specified range. Consider the following code of a setter method:
|
By using getter and setter, the programmer can control how their variables are accessed and updated, such as changing the value of a variable within a specified range. Consider the following code of a setter method:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
public void setNumber(int num) {
|
public void setNumber(int num) {
|
||||||
if (num < 10 || num > 100) {
|
if (num < 10 || num > 100) {
|
||||||
@ -51,13 +52,16 @@ public void setNumber(int num) {
|
|||||||
this.number = num;
|
this.number = num;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
This ensures the value of `number` is always set between 10 and 100. If the programmer allows the variable number to be updated directly, the caller can set any arbitrary value to it:
|
|
||||||
|
This ensures the value of `number` is always set between 10 and 100. If the programmer allows the variable `number` to be updated directly, the caller can set any arbitrary value to it:
|
||||||
|
|
||||||
```java
|
```java
|
||||||
obj.number = 3;
|
obj.number = 3;
|
||||||
```
|
```
|
||||||
|
|
||||||
This violates the constraint for values ranging from 10 to 100 for that variable. Since we don't expect that to happen, hiding the variable number as private and using a setter prevents it.
|
This violates the constraint for values ranging from 10 to 100 for that variable. Hiding the variable as private and only modifying it through the setter, prevents it from violating the constraint.
|
||||||
On the other hand, a getter method is the only way for the outside world to read the variable’s value:
|
|
||||||
|
Since the variable is now private, a getter method is the only way for the outside world to read the variable’s value:
|
||||||
```java
|
```java
|
||||||
public int getNumber() {
|
public int getNumber() {
|
||||||
return this.number;
|
return this.number;
|
||||||
|
Reference in New Issue
Block a user