What is the convention for using upper and lower case names in Java?

Asked

Viewed 3,199 times

2

I once read somewhere about the best practices of using upper and lower case when you name the methods, attributes, variables... in Java, someone could refresh my memory?

  • 1

    http://www.oracle.com/technetwork/java/codeconventions-135099.html

  • @Articuno This question is ancient, and similar to this other. I am inclined to close as duplicate, but since my vote (as well as yours) would close the question immediately, I thought it best to ask your opinion. You think you’re duplicate?

  • @Victorstafusa’s explanation of the other is much more detailed and complete, had not noticed that they could be duplicates. For me, it makes perfect sense the same closure, even to facilitate to locate later.

  • 1

    @Articuno I closed as duplicate.

2 answers

8


Packages ()

The package name prefix must be unique, must always be written in full-ASCII lowercase letters and must be one of the top-level domain names, currently with, edu, Gov, mil, net, org, two-letter codes identifying countries as specified in ISO 3166, 1981. Subsequent components of the package name vary according to an organization’s own internal nomenclature conventions. Such conventions may specify that certain components of the directory name there be division, department, project, machine, or login names.

Example:

com.sun.eng 
com.apple.quicktime.v2 
edu.cmu.cs.bovik.cheese

Classes

Class names should be nouns, in upper and lower case, with the first letter of each inner word capitalized. Try to keep your class names simple and descriptive. Always avoid words-linked, avoid all acronyms and abbreviations, be semantic.

Example:

class Raster; 
class ImageSprite;

Interfaces

Interface names should be used with uppercase first letters as class names.

Example:

interface RasterDelegate;
interface Storing;

** Methods (Methods)**

Methods should be verbs, with the lowercase letter first, with the first letter of each inner word capitalized.

Example:

run(); 
runFast(); 
getBackground();

Variables

Variable names should not start with underscore _ or dollar sign $ characters, even though both are not allowed. Variable names must be short but significant. The choice of a variable name must be mnemonic, i.e., designed to indicate to the casual observer the intention of its use. A character names of variables should be avoided, except for temporary "disposable" variables. Common names for temporary variables are i, j, k, m, n and for integers, c, d, e and for characters

Example:

int i; 
char c; 
float myWidth; 

Constants

The names of variables declared as class constants and ANSI constants shall be all in upper case letters with words separated by underscores ("_").

static final int MIN_WIDTH = 4; 
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;

4

Java has a very simple convention, essentially everything should be camelCase (variables at any location, including attributes, methods, packages).

Except type names (class, interface, enumeration) which must be Pascalcase.

And constants, including those within the Enum, it should all be uppercase (ALL_CAPS), where it is the only place where the use of underline to separate names.

Remembering camelCase traditional starts with lowercase and the other words of the name continue beginning with uppercase. This difference already gives the notion of being a different word. It can have any number of words. Some people know this convention by other names.

Pascalcase only differs by the fact that the first word of the name is already capitalized as well. There are other names for this convention.

Note that acronyms should use camelCase as well. So use valorIcms and not valorICMS.

Wikipedia article with examples.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.