1.2.9 How to Define a Field

Fields are 'attributes' that define a class. In other words, you are a Person class and your fields are things like your weight, eye color, skin color, etc. When we define, fields, then we are saying that a particular class should be composed of certain attributes.

To define a field in a class, first navigate to the class you want to put the field in. In this example we will add a Price to the Food class.

When defining a field, ensure it is the first thing in a class.

In the image above:

  1. Is the visibility of a field.
  2. Is the field type.
  3. Is the name of the field.

Capitalization is important when defining fields. For right now, note that primitive fields type should be lowercase, custom types should be uppercase and the names of your fields should be uppercase.

In a class diagram, the newly defined field would look like:

In the Solution Explorer, the newly defined field would look like: