add classes and methods lesson - #117
Conversation
|
🌐 Preview URL: https://pr-117.frcsoftware.pages.dev |
6a9a47c to
6576c3c
Compare
24d72a4 to
6ebecc0
Compare
6ebecc0 to
cd87076
Compare
Daniel1464
left a comment
There was a problem hiding this comment.
I like the changes! Here's some small nitpicks
| This means that they can only be accessed by methods in the same class. | ||
|
|
||
| So far, the `x` and `y` fields each belong to a specific `Point` object. | ||
| Sometimes, though, we want a value that's shared by every object of a class, instead of one copy per object. |
There was a problem hiding this comment.
I know that static members are traditionally described this way, but i personally think that it kind of misses the point. For instance, saying that Math.clamp() is a method that belongs to all objects of Math doesn't really make sense, since you can never create instances of Math anyways.
Personally, I would change it to "static fields are variables that are related to the class, but don't belong to any particular object."
| Notice that we used the `final` keyword to make these fields **immutable**. | ||
| This means that once they are created, they cannot be changed. | ||
| Immutable fields are also called **constant** fields. | ||
| A general rule of thumb is to make all fields `final` unless you have a good reason not to, |
There was a problem hiding this comment.
This should probably be changed to "A field is a variable that belongs to instances of this class".
Another thing to add would be "Notice that these fields don't have values assigned to them! That's because the values of x and y are different between every Point object. In later sections, we'll define code that sets the values of these fields."
| Then, we're going to add three fields. | ||
| A **field** is a variable that belongs to a class, and stores part of an object's state. | ||
| Every `Point` object will have its own copy of each field, so two different `Point` objects can have different coordinates. | ||
| Firstly, we add two `double` fields, x and y, which represent the point's coordinates. |
There was a problem hiding this comment.
You could add "This means that every Point created will have an x and y attribute" to contrast with static fields.
|
|
||
| ``` | ||
|
|
||
| Finally, we're going to define a `norm` method that returns the length of the `Point` object. |
There was a problem hiding this comment.
Saying "the distance of the point from 0,0" instead of "length of the point object" would be slightly clearer.
|
|
||
| ``` | ||
|
|
||
| We create new `Point` objects using the `new` keyword followed by the constructor. |
There was a problem hiding this comment.
I think this needs to be earlier, probably after the constructor definition and before the method definitions? Since the method definitions also use new Point(), which might confuse people if this isn't explained before
16c9f94 to
a7a2b43
Compare
Description
I use two example classes here to demonstrate value classes and classes with mutable state.
Part of #16.
Meta
Merge checklist: