From abb98927b0db98b33529d949d20e63295b862c9f Mon Sep 17 00:00:00 2001 From: Shooflower Date: Sun, 25 Nov 2018 23:26:06 -0500 Subject: [PATCH] Spelling/grammar fixes and some clarification (#23518) * Spelling/grammar fixes and some clarification * Fixed the 'number' variable in the text --- guide/english/java/getters-and-setters/index.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/guide/english/java/getters-and-setters/index.md b/guide/english/java/getters-and-setters/index.md index e6786a4f90..ab353e2f65 100644 --- a/guide/english/java/getters-and-setters/index.md +++ b/guide/english/java/getters-and-setters/index.md @@ -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 variable’s 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 variable’s value: ```java public int getNumber() { return this.number;