|
| 1 | +# static |
| 2 | +static - is a keyword that defines properties or methods that belong to a class itself rather than the object created from that class |
| 3 | +class owns anything static and not the the object |
| 4 | + |
| 5 | +## I practiced static keyword using the examples below: |
| 6 | +### Example 1: |
| 7 | +```javascript |
| 8 | +class MathUtilities{ |
| 9 | + static PI = 3.14159 |
| 10 | + |
| 11 | + static getDiamater(radius){ |
| 12 | + return radius * 2; |
| 13 | + } |
| 14 | + static getCircumference(radius){ |
| 15 | + return (this.PI * radius * 2).toFixed(2); |
| 16 | + } |
| 17 | + static getArea(radius){ |
| 18 | + return (this.PI * radius * radius).toFixed(2); |
| 19 | + } |
| 20 | +} |
| 21 | +console.log(MathUtilities.PI); |
| 22 | +console.log(MathUtilities.getDiamater(15)); |
| 23 | +console.log(MathUtilities.getCircumference(7)); |
| 24 | +console.log(MathUtilities.getArea(18)); |
| 25 | +``` |
| 26 | + |
| 27 | +#### output: |
| 28 | +```javascript |
| 29 | +console.log(MathUtilities.PI); // 3.14159 |
| 30 | +console.log(MathUtilities.getDiamater(15)); // 30 |
| 31 | +console.log(MathUtilities.getCircumference(7)); // 43.98 |
| 32 | +console.log(MathUtilities.getArea(18)); // 1017.88 |
| 33 | +``` |
| 34 | + |
| 35 | +### Explanation of the code: |
| 36 | +- I created a class called *MathUtilities* to hold math-related utilities like PI, diameter, circumference, and area. |
| 37 | +- I used the *static* keyword so that I could call all properties and methods **directly on the class**, without creating any object instance. |
| 38 | +- I declared a static property *PI* and gave it the value 3.14159 so that I could reuse it in all circle calculations. |
| 39 | +- I added a static method *getDiameter(radius)* so that the code can return the diameter of a circle by simply multiplying the radius by 2. |
| 40 | +- I added a static method *getCircumference(radius)* so that the code can return the circumference using the formula *this.PI * radius * 2*, and I used *this.PI* to refer to the static *PI* within the class. |
| 41 | +- I added a static method *getArea(radius)* to return the area of a circle using the formula *this.PI * radius * radius*, formatted to 2 decimal places using .*toFixed(2)*. |
| 42 | +- I logged all the methods to the console so I could confirm the calculations worked without needing to instantiate the class. |
| 43 | + |
| 44 | +### Example 2: |
| 45 | +```javascript |
| 46 | +class User{ |
| 47 | + static userCount = 0; |
| 48 | + |
| 49 | + constructor(username){ |
| 50 | + this.username = username; |
| 51 | + User.userCount ++; |
| 52 | + } |
| 53 | + |
| 54 | + static getUserCount(){ |
| 55 | + console.log(`There are ${User.userCount} users online`); |
| 56 | + } |
| 57 | + |
| 58 | + sayHello(){ |
| 59 | + console.log(`My username is ${this.username}`); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +const user1 = new User("Linus"); |
| 64 | +const user2 = new User("Annette"); |
| 65 | +const user3 = new User("Laura"); |
| 66 | + |
| 67 | +user1.sayHello(); |
| 68 | +user2.sayHello(); |
| 69 | +user3.sayHello(); |
| 70 | + |
| 71 | +console.log(user1.username); |
| 72 | +console.log(user2.username); |
| 73 | +console.log(user3.username); |
| 74 | + |
| 75 | +User.getUserCount(); |
| 76 | +console.log(User.userCount); |
| 77 | +``` |
| 78 | + |
| 79 | +#### output: |
| 80 | +```javascript |
| 81 | +user1.sayHello(); // My username is Linus |
| 82 | +user2.sayHello(); // My username is Annette |
| 83 | +user3.sayHello(); // My username is Laura |
| 84 | + |
| 85 | +console.log(user1.username); // Linus |
| 86 | +console.log(user2.username); // Annette |
| 87 | +console.log(user3.username); // Laura |
| 88 | + |
| 89 | +User.getUserCount(); // There are 3 users online |
| 90 | +console.log(User.userCount); // 3 |
| 91 | +``` |
| 92 | + |
| 93 | +### Explanation of the Code: |
| 94 | +- I created a class called User to model users, each with a unique *username*. |
| 95 | +- I used a *static* property *userCount = 0* so that the class can keep track of how many users have been created. |
| 96 | +- I added a *constructor(username)* method so that each time I create a new user, it stores their username in *this.username*, and then increments the global *userCount* by 1. |
| 97 | +- I created a static method *getUserCount()* so that the code can print out how many users exist without needing an instance. |
| 98 | +- I created an instance method *sayHello()* so each user object can introduce itself using its own *username*. |
| 99 | +- I instantiated three users *(user1, user2, and user3)* using the *new* keyword so that each user object has its own data. |
| 100 | +- I called *sayHello()* on each user so that they could introduce themselves using their unique username. |
| 101 | +- I accessed *User.userCount *and called *User.getUserCount()* so that I could confirm the static counter worked as expected. |
| 102 | + |
| 103 | +## What I learnt from the examples demonstrated above |
| 104 | +1. How to declare static properties and methods so they live on the class, not on each object. |
| 105 | +2. How this inside a static context refers to the class, while inside an instance method it refers to the current object. |
| 106 | +3. How to blend static and instance logic in one class—for example, tracking how many objects have been created. |
| 107 | +4. When to choose utility-only classes (all static) versus regular classes with per-object state. |
0 commit comments