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.
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.
bark()
name
An instance method can use the object's ___ directly by name.
How do you call the bark method on an object d?
bark
d
bark() prints the name field then says woof. Type what this prints:
says woof
Dog d = new Dog(); d.name = "Rex"; d.bark();
True or false?