How to Edit an Attribute's Initial Value
Initial value defines the default value for attribute when the owning object is instantiated. You can give a text value for initial value, or select a public attribute from other classes. In this tutorial, you can study how to define default value for attribute of a class and then share the same default value with another class.
- Create a new project by selecting Project > New from the application toolbar. In the New Project window, enter A Simple Model as the project name and click Create Blank Project.
- Create a class diagram by selecting Diagram > New from the application toolbar. Select Class Diagram and click Next.
- Click OK to confirm.
- Create a class by selecting Class from the diagram toolbar in advance and clicking it on the diagram. Name it COLOR. In some programming languages, a constant class is named in uppercase.
- Although COLOR is a constant class, you can set it to be a static class. Right-click on the class and select Open Specification... from the pop-up menu.
- In the Class Specification window, open the Class Code Details tab and check final and static. Next, click the OK button.
- COLOR contains several attributes for different colors. Let's create an attribute for red. Right-click on COLOR and select Add > Attribute from the pop-up menu.
- Enter +red : int = 1 and press Enter to confirm. Entering + means you set it as public, thereby, it becomes accessible to other classes. Here red is the name of the attribute while 1 is the default value.
- Similarly, enter green : int = 2 and blue : int = 3 respectively. The image below shows the result:
- Since the three colors are static, you'd better set their scope to be a classifier. Select all three attributes, right-click on them, and select Model Element Properties > Scope > Classifier from the pop-up menu.
- Now, create another class through the diagram toolbar and name it Shape.
- Add an attribute and then name it color : int, press Enter, and then Esc to confirm.
- Let's customize the default color of the shape as green. You have to set the initial color for the color attributes as COLOR.green in advance. Right-click on the attribute color and select Open Specification... from the pop-up menu.
-
In the Attribute Specification window, open the General tab. Press the upside-down arrow in the Initial value field to select green : int in the tree. Click OK to close the specification window. Note that only public attributes are available to select as the initial color.
The result of the Shape class is shown as follows: