- Java 9 with JShell
- Gastón C. Hillar
- 829字
- 2021-07-09 18:46:47
Recognizing variables and constants
We know the information required for each of the shapes to achieve our goals. Now, we have to design the classes to include the necessary fields that provide the required data to each instance. We have to make sure that each class has the necessary fields that encapsulate all the data required by the objects to perform all the tasks based on our application domain.
Let's start with the Circle
class. We need to know the radius for each instance of this class, that is, for each circle object. Thus, we need an encapsulated variable that allows each instance of the Circle
class to specify the value for the radius.
Note
The variables defined in a class to encapsulate the data for each instance of the class in Java 9 are known as fields. Each instance has its own independent value for the fields defined in the class. The fields allow us to define the characteristics for an instance of the class. In other programming languages that support object-oriented principles, these variables defined in a class are known as attributes.
The Circle
class defines a floating point field named radius
, whose initial value is equal to 0
for any new instance of the class. After we create an instance of the Circle
class, it is possible to change the value of the radius
attribute. Thus, our circle can become smaller or larger after we created it.
Tip
Note the usage of Camel case for field names. Camel case means that the first letter is lowercase, and then, the first letter for each word that composes the name is capitalized, while the other letters are in lowercase. It is a coding convention in Java for both variables and fields. For example, we use the name radius
for the field that stores the value of the radius and we will use lengthOfSide
for the property that stores the value of the length of side in other classes that require this data.
Imagine that we create two instances of the Circle
class. One of the instances is named circle1
and the other circle2
. The instance names allow us to access the encapsulated data for each object, and therefore, we can use them to change the values of the exposed fields.
Java 9 uses a dot (.
) to allow us to access the properties of instances. So, circle1.radius
provides access to the radius for the Circle
instance named circle1
, and circle2.radius
does the same for the Circle
instance named circle2
.
Tip
Note that the naming convention makes it easy for us to differentiate an instance name, that is, a variable, from a class name. Whenever we see the first letter in uppercase or capitalized, it means that we are talking about a class, as in Circle
or Rectangle
.
We can assign 14
to circle1.radius
and 39
to circle2.radius
. This way, each Circle
instance will have a different value for the radius
field.
Now, let's move to the Rectangle
class. We have to define two floating point fields for this class: width
and height
. Their initial values will also be 0
. Then, we can create four instances of the Rectangle
class named rectangle1
, rectangle2
, rectangle3
, and rectangle4
.
We can assign the values summarized in the following table to the four instances of the Rectangle
class:
This way, rectangle1.width
will be equal to 141
, while rectangle4.width
will be equal to 84
. The rectangle1
instance represents a rectangle with width
of 141
and height
of 281
.
The following table summarizes the floating point fields defined for each of the nine classes that we need for our Web Service backend code:
Tip
The fields are members of their respective classes. However, fields aren't the only members that classes can have.
Note that six of these classes have the same field: lengthOfSide
, specifically, the following six classes: EquilateralTriangle
, Square
, RegularPentagon
, RegularHexagon
, RegularOctagon
, and RegularDecagon
. We will pe deep into what these six classes have in common later and take advantage of object-oriented features to reuse code and simplify our Web Service maintenance. However, we are just starting our journey, and we will make improvements as we learn additional object-oriented features included in Java 9. In fact, let's remember we are learning about the application domain and that we are still not experts in 2D shapes.
The following image shows a UML (Unified Modeling Language) class diagram with the nine classes and their fields. This diagram is very easy to understand. The class name appears on the top of the rectangle that identifies each class. A rectangle below the same shape that holds the class name displays all the field names exposed by the class with a plus sign (+) as a prefix. This prefix indicates that what follows it is an attribute name in UML and a field name in Java 9. Take into account that the next UML diagram doesn't represent the best organization for our classes. It is just the first sketch.