4️⃣

1.4 Public, Private, and Static Fields

1.4 Public, Private, and Static Fields

Static Keyword

Let’s return to the example of the Student class. Suppose we would like to set the score ranges for the letter-scale grading system (A, B, C, D, F). These score ranges would apply to all students. So, it wouldn’t make sense to store them for each student. Instead, we only need to store it once for the Student class. Consider the following code, especially the bolded part:
class Student { //A = 90 or higher, B = 80 ~ 89, C = 70 ~ 79, etc. static A = 90, B = 80, C = 70, D = 60; constructor(name, grade) { this.name = name; this.grade = grade; this.marks = []; } }
Here, A, B, C, D are static properties, as indicated by the static keyword. Static properties belong to classes, not objects, so they can only be called on a class.
Similarly, we can create static methods in classes as well. For example, suppose we have to change the score ranges for the different letter grades. Then, we can define static methods as below:
class Student { static A = 90, B = 80, C = 70, D = 60; constructor(name, grade) { this.name = name; this.grade = grade; this.marks = []; } //static method changes minimum score for the letter grade passed static changeScoreRange(letter, minimum) { switch(letter) { case “A” : A = minimum; break; case “B” : B = minimum; break; case “C” : C = minimum; break; case “D” : D = minimum; break; } }
We can only can static methods on classes. We cannot call static methods on instances of a class. Thus, we may call the changeScoreRange function as follows:
Student.changeScoreRange(“A”, 87);
But we may not try the following:
//this code will error let stud1 = new Student(“Mark”, 7); stud1.changeScoreRange(“A”, 87);

Public and Private Fields

So far, we accessed object properties and methods at will. Object properties and methods that we can access anywhere in our code are public. All properties and methods are public by default.
However, we can easily change that: we can make properties and methods only accessible in their class. Such methods and properties are private. Private properties can only be accessed by methods in their class, and private methods can only be called by other methods in their class.
In general, we want our object properties to be private. Otherwise, we may easily accidentally change their values. To declare a private property, add a hash symbol ‘#’ in front of the property name:
class Student { static #A = 90, #B = 80, #C = 70, #D = 60; constructor(name, grade) { this.name = name; this.grade = grade; this.marks = []; } }
Now, the A, B, C, D, variables are private (and static). So, if we try to access them outside of the Student class, our code will error:
//error console.log(Student.#A);
How do we access properties if they are private? That’s what getter methods are for (See section 7.3). Usually, object methods are public, but sometimes, we would create private methods as well. For example, we can create a private method in the Student class for converting percentages marks into letter grades:
class Student { static #A = 90, #B = 80, #C = 70, #D = 60; constructor(name, grade) { this.name = name; this.grade = grade; this.marks = []; } #toLetterGrade(percentMark) { //no need for this.A here, since A is a static variable if (percentMark >= A) { return “A”; } else if (percentMark >= B) { return “B”; } else if (percentMark >= C) { return “C”; } else if (percentMark >= D) { return “D”; } else { return “F”; } } }
One example of our private method in action is in the method below, printLetterGrades:
class Student { //add the method below to the student class //Here, printLetterGrades is public printLetterGrades() { let outStr = “”; for (let i=0; i<this.marks.length; i++) { outStr += toLetterGrade(this.marks[i]) + “ “; } console.log(outStr); } }
Of course, we can’t try to call on our private method, toLetterGrade, in any way when we’re outside of the Student class. Otherwise, we’ll have an error.

Previous Section

Next Section

 
⚖️
Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.