Naming convention is a rule to follow while naming the identifiers like class, package, interface, variable, constant, methods.
Conventions make programs more understandable and easier to read. These conventions are suggested and mutually agreed upon.
If you don’t follow the naming conventions, the code will run without errors (it may fail in a few cases) as they are conventions (good to follow), not grammar/rules.
Naming of Identifiers
-
Package: Package names should be unique. Names are written in all-lowercase ASCII letters. The first component should be one of the top level domain names (com, edu, gov, mil, net, org). Subsequent components of the package name vary according to an organization’s own internal naming conventions. Avoid using underscores or hyphens in names. eg
com.sun.eng
com.apple.quicktime.v2
-
Classes: Class name should be a noun in mixed case with the first letter of each internal word capitalized. It should start with the upper case letter. Name should be simple and descriptive. Unless the acronyms are very popular (like HTTP, URL, etc), use appropriate words. eg
public class User
,public class Post
-
Interfaces: Convention like class. It should start with uppercase letter and should be in mixed case with the first letter of each internal word capitalized. eg
interface Storing
-
Methods: The name should be verb. Method names should be written in camelCase starting with a lowercase letter (means first letter lowercase and subsequently first letter of each internal word capitalized). eg
public void createPost()
-
Variable: The name should start with a lowercase letter. Names should not start with
underscore _
,ampersand &
ordollar sign $
characters, even though these are allowed. Names should be short yet meaningful. If the name contains multiple words, start it with the lowercase letter followed by an uppercase letter such asfirstName
,lastName
. One-character variable names should be avoided except for temporary “throwaway” variables. -
Constants: Names should be in all uppercase letters like
AGE
,MAX_RETRIES
. If the name contains multiple words, it should be separated by anunderscore _
.