All about 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 project with starting VP-UML. Select File > New Project from the main menu. In the New Project dialog box, enter in A Simple Model as project name and then click Create Blank Project button to continue.

- Create a class diagram by right clicking on Class Diagram in Diagram Navigator and selecting New Class Diagram from the pop-up menu.

- Create a class by selecting Class from the diagram toolbar in advance and clicking it on the diagram. Name it as COLOR. In some programming languages, a constant class is named in upper case.

- Although COLOR is 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 dialog box, open Class Code Details tab and check final and static. Next, click 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 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 classifier. Select all the three attributes, right click on them and select Model Element Properties > Scope > Classifier from the pop-up menu.

- Now, create another class through diagram toolbar and name it as Shape.

- Add an attribute and then name it as color : int, press Enter and then Esc to confirm.

- Let customize the default color of 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 dialog box, open 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 dialog box. Note that only public attributes are available to select as the initial color.
The result of Shape class is shown as follows:
Rate this Article
Click on one of the stars below to rate this article from 1 (lowest) to 5 (highest).
One Moment Please
Thanks for rating this article.
Comments (0)