What is the appropriate naming convention for instance variables?

advertisements

In my introductory programming course, my teacher always uses the following naming convention when declaring instance variables in his code:

public class Snowman {
    private Ellipse _top;
    private Ellipse _middle;
    private Ellipse _bottom;

public Snowman() {
        _top = new Ellipse();
        _top.setColor(Color.WHITE);
        _top.setFrameColor(Color.BLACK);
        _top.setFrameThickness(1);
        _top.setSize(80, 80);
        _middle = new Ellipse();
        _middle.setColor(Color.WHITE);
        _middle.setFrameColor(Color.BLACK);
        _middle.setFrameThickness(1);
        _middle.setSize(120, 120);
        _bottom = new Ellipse();
        _bottom.setColor(Color.WHITE);
        _bottom.setFrameColor(Color.BLACK);
        _bottom.setFrameThickness(1);
        _bottom.setSize(160, 160);
    }
}

In the course textbook, however, instance variables do not start with an underscore, but rather follow the same naming convention as that of primitive type variables (int revolutionsPerMinute). When I did an online search, I found several sources, including the online Javadoc, that cited the same convention as my textbook. As I could not reproduce the naming conventions my teacher follows, I am skeptical as to whether his convention is legitimate. Is the convention taught my teacher even existant, if not widely accepted?


Normally the standard convention in Java is camel case where you start the variable with lower case and using upper case for the first letter of every other word. For example:

int myVariable;
int mySecondVariable;

But note that these are conventions not rules. So anyone is free to use any convention, but it is recommended to use the standard convention to make the code readable for anyone.