Example. Fields

[comp1110-82]

Say we want our Person class from earlier to have two fields: one representing the name of the person, and one representing their age. Names would typically be represented as Strings, and in our case, we only care about completed years of age, so we use int as the type for that field.

class Person { int age; String name; }

We can then create a new Person value and write to its fields:

Person p = new Person(); p.name = "Timmy"; p.age = 5;
bars magnifying-glass caret-down plus minus arrow-right xmark