Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java conventions for field name as keyword [closed]

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_;
}
like image 831
graczun Avatar asked Oct 30 '25 21:10

graczun


2 Answers

The general convention (nothing official, but for experience) in this cases is to add the name of the class as a suffix.

private boolean publicEvent

like image 96
carborgar Avatar answered Nov 02 '25 11:11

carborgar


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)
like image 23
Davide Lorenzo MARINO Avatar answered Nov 02 '25 13:11

Davide Lorenzo MARINO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!