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:
Shooflower
2018-11-25 23:26:06 -05:00
committed by Manish Giri
parent 15e6af1d67
commit abb98927b0

View File

@ -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:
```java
public stativ void main(String[] args) {
public static void main(String[] args) {
Vehicle v1 = new Vehicle();
v1.setColor("Red");
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?
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
public void setNumber(int num) {
if (num < 10 || num > 100) {
@ -51,13 +52,16 @@ public void setNumber(int 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
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.
On the other hand, a getter method is the only way for the outside world to read the variables value:
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.
Since the variable is now private, a getter method is the only way for the outside world to read the variables value:
```java
public int getNumber() {
return this.number;