Object-Oriented Java - 10.Methods: II

I don’t know what I did wrong… Please help me:(

you should give space after the ending curly braces!

---
-
dog
-
---

In the Language families Project,
https://www.codecademy.com/courses/learn-java/projects/java-language-families
when we are accessing a instance field for a method in the same file do we have to use “this.field”?
Just only using field works right?

Can you rephrase your question? Are you asking about accessing an instance method or instance field?

Instance fields are variables that belong to an instance of the class. What do you mean by “for a method”?

accessing instance fields.

There are a few instances where this is required.

  • To differentiate between an instance variable another variable or method parameter with the same name.
  • To pass the current object to another object.
  • And a few more other uses you likely won’t be needing for now.

So in short, to answer your question, this is not necessary to access an instance field in a method unless that method contains variables or parameters that have the same name as one of the object’s instance variables.

More on this here.

1 Like