Skip to main content
Engineering LibreTexts

1.2: Attributes

  • Page ID
  • Variables that belong to an object are usually called attributes, but you might also see them called “fields”. To access an attribute of an object, Java uses dot notation. For example:

    int x = blank.x;

    The expression blank.x means “go to the object blank refers to, and get the value of the attribute x.” In this case, we assign that value to a local variable named x. There is no conflict between the local variable named x and the attribute named x. The purpose of dot notation is to identify which variable you are referring to unambiguously.

    You can use dot notation as part of an expression. For example:

    System.out.println(blank.x + ", " + blank.y);
    int sum = blank.x * blank.x + blank.y * blank.y;

    The first line displays 3, 4; the second line calculates the value 25.