DEV Community

Mark Rubin
Mark Rubin

Posted on • Updated on

Naming Conventions

This is part of a brief series on Good Habits For Java Programmers.

Java allows you a lot of flexibility in how you name classes, variables, methods, etc. But there are conventions that Java programmers follow. Obeying these conventions is important: it makes it much easier for all of us to comprehend each others' programs quickly and easily.

Class and Interface names begin with an uppercase letter

Your class and interface names should always begin with an uppercase letter. Java does not require this -- you won't get a syntax or compiler error if you start your class name with a lowercase letter. But it's just how things are done. Not following this convention will ultimately lead to confusion.

Variable, method, parameter names should always begin with a lowercase letter

Again, this is not required by Java, and your Java programs will compile and run if you break this convention. Nonetheless, convention has value.

Use CamelCase for class, variable, parameter, and method names

Examples:
double sellerCost;
double buyerPrice;
double getProfit(double sellerCost, double buyerPrice)
void displayDataAsTable()
class ProfitCalculator

Meaningful names for your classes, variables, methods, and parameters are generally multiple words stuck together in sequence to make a small phrase that describes what they do or what they're for. You should be able to get the idea from the examples above.

If it's a class or interface name, you start with an uppercase letter; if it's a variable, method, or parameter name you start with a lowercase letter. Then you lowercase the rest of the letters in the first of the jammed together words, and then capitalize the first letter of the next word, lowercase its remaining letters, capitalize the first letter of the next word, and so on. The examples are easier to understand than the explanation.

There are other rules

These cover most everything you'll use early on.

There are rules for so-called constants in Java, which use a capitalized SNAKE_CASE: e.g.

public static final double MAX_PRICE = 100.0;
Enter fullscreen mode Exit fullscreen mode

and more constructs. I only mention this for completeness. Don't sweat them. Focus on the rules for class names, variables, methods, and parameters, and you'll be in great shape.

Top comments (0)