Task 1 of 9 · 0 solved · 9 to go0%
You can read the problem, but answers are checked only for signed-in students. Sign in to answer →

4.3 Instance methods — a method that uses a field

Lesson

Behavior that uses the object's data

A class holds data in fields and behavior in methods. A method written inside a class — an instance method — can use that object's fields directly by name. This is how an object acts on its own data.


Worked example

class Dog {
    String name;

    void bark() {
        System.out.println(name + " says woof");   // uses the field
    }
}

Dog d = new Dog();
d.name = "Rex";
d.bark();          // Rex says woof

▶ Try it: bark() uses the object's name field. Run this, then change the name and re-run.



An instance method can use the object's ___ directly by name.


How do you call the bark method on an object d?


bark() prints the name field then says woof. Type what this prints:

Dog d = new Dog();
d.name = "Rex";
d.bark();

True or false?