0
public class Tester {

   private double length;
   private double width;
   private double height;

   public Tester(double ________1, double _________2, double _________3){

      length = _________1
      width =  _________2
      height = _________3

   }//Tester constructor
}//Tester class

Above is a simple sample code and I was wondering if there is a "good" naming convention for naming variables when making classes?

For example what would be good names for _____1, _____2, _____3?

Would it be something like testerLength, testerWidth, testerHeight?

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • [The original Java style guide](http://www.oracle.com/technetwork/java/codeconvtoc-136057.html) and [Google's style guide](https://google-styleguide.googlecode.com/svn/trunk/javaguide.html). – Kenster Feb 21 '15 at 20:02

1 Answers1

1

The names of the arguments should be very meaningful, because you will see them in the Javadoc. You have multiple options here, I will give you an example for a few:

an a before every argument:

public Tester(double aLength, double aWidth, double aHeight) {
    length = aLength;
    width = aWidth;
    height = aHeight;
}

as @Cyber mentioned: using an _:

public Tester(double length, double _width, double _height) {
    length = _length;
    width = _width;
    height = _height;
}

and my personal favorite, just use the same name and the scope:

public Tester(double length, double width, double height) {
    this.length = length;
    this.width = width;
    this.height = height;
}
reckter
  • 586
  • 5
  • 12
  • Wait you can do that? Why would anyone do differently? Also is it true to believe that the "this.length" references the previous private variables, while the "length" is referencing the argument? – Leinad Fpmek Feb 21 '15 at 19:35
  • Jup. `this` is just a keyword for the current class you are in. `length`references to the argument, because the argument declaration overwrites the scope of the class. – reckter Feb 21 '15 at 19:39