Implied this

class Car {

  String licensePlate = "";     // e.g. "New York 543 A23"
  double speed        = 0.0;    // in kilometers per hour
  double maxSpeed     = 123.45; // in kilometers per hour
  
  void floorIt() {
    speed = maxSpeed;
  }

}

Within the Car class, you don't absolutely need to prefix the field names with this. like this.licensePlate or this.speed. Just licensePlate and speed are sufficient. The this. may be implied. That's because the floorIt() method must be called by a specific instance of the Car class, and this instance knows what its data is. Or, another way of looking at it, the every object has its own floorIt() method.

For clarity, I will use an explicit this today, and I recommend you do so too, at least initially. As you become more comfortable with Java, classes, references, and OOP, you will be able to leave out the this without fear of confusion. Most real-world code does not use an explicit this.


Previous | Next | Top | Cafe au Lait

Copyright 1997-1999 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified October 1, 1999