Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

7
  • public int range() { return this.mpg * this.fuelcap; // why is this. needed here? Can't we achieve the same thing with mpg * fuelcap } Commented Jun 14, 2016 at 5:45
  • @Rio You are right, it is not needed here, because those two variables are clearly identifiable to be the field variables. Commented Jun 14, 2016 at 5:52
  • @Rio You can. this. is optional, but it is a good practice. It helps document the difference between accessing variables/parameters vs. fields, so other people looking at the code will know for sure what is being accessed. This is especially true for larger methods, where many parameters and variables may exist. I find it a good code practice to always do it, and IDEs like Eclipse can be told to help enforce such a "code style", using compiler error/warning "Unqualified access to instance field". Commented Jun 14, 2016 at 5:55
  • @Andreas You mean i can set Eclipse to tell me if my code style is not proper? That's awesome! Can you tell me how to do that? Commented Jun 14, 2016 at 6:02
  • @Rio You can define "Save Actions" in Eclipse: Window -> Preferences -> Java -> Editor -> Save Actions. Now go to the tab Code Style and enable "Use final modifier where possible". So, eclipse will add the final modifier keyword where possible each time you save your code. Commented Jun 14, 2016 at 6:12