Search code examples
javaconventions

Java conventions for field name as keyword


Is there any convention for fields with name same as java keyword? For example want to create field with name "public":

public class Event{
   private boolean public_;
}

Solution

  • There is no standard convention since the code convention document released by sun in 1999.

    The only reference to variable names on that document is the following (chapter 9):

    Except for variables, all instance, class, and class constants are in mixed case with a lowercase first letter. Internal words start with capital letters. Variable names should be short yet meaningful. The choice of a variable name should be mnemonic— that is, designed to indicate to the casual observer the intent of its use. One-character variable names should be avoided except for temporary “throwaway” variables. Common names for temporary variables are i, j, k, m, and n for integers; c, d, and e for characters.

    As you can see there is no reference to how to use a keyword as variable name. The only tip that I can suggest are:

    • avoid it and use a different word (a synonym)
    • if you choose to use a keyword, be coherent and use the same convention for the whole code (for example prefix it with _ or suffix it with the name of the class)