Question of the Week #74
What are common naming conventions used in Java programs?
17 messages · Page 1 of 1 (latest)
What are common naming conventions used in Java programs?
names should generally be lowercase.
package mypackage
Larger projects may need to separate packages more granularly.
package services.dj45x.mypackage
are most commonly CamelCased.
class CustomerModel
Same applies to interfaces
interface EnumerableTypes
should use mixedCase.
String getName()
should be uppercase
static final int DEFAULT_WIDTH
variables lower case
methods camelcase
constants upper case
classes first letter uppercase
methods are verbs
classes and variables are nouns
In Java we have common naming conventions like this:
All of the names also have to describe the function of the called variable/method/class. E.g: a method which returns the calculated time, a user needed, to complete a task, needs more info than just: getTime(). It would be better to call it: getCalculatedTaskTime()
Do you mean UpperCamelCase for class names and lowerCamelCase for most other variables and identifiers?
There are 3 naming conventions used in Java programs:
Idk if I understand it the right way but there are Getter/Setter Methods that are named by "Get..." Or "Set..."
if the answers are to be submitted here
the 1st is most famous camel case
example = myController , theBook
2.Upper Camel Case -
it is used for naming classes names , implementation names and all
example = HttpServletRequest( i just learnt this xD) , The Book , etc
for now i can only recognise this
thankyou
Universal/official conventions:
UpperCamelCaseUPPER_SNAKE_CASElowerCamelCasedot.separated.lower.case.components, though sometimes camelCase components are usedOthers:
A less common, but still "socially acceptable" convention is to prefix instance fields with an underscore, in order to distinguish them from local variables, such as _myField.
Generally speaking, types and fields should be named as nouns, e.g. String, Customer, DATE_FORMAT, calculationResults. Method names should start with verbs, e.g. execute, formatDate, calculateResults.
Unit tests often have more information encoded into the name, in order to communicate the pre-conditions and expected results of the test case (particularly within test result reports). The following are some common-ish naming conventions for test methods:
testMethodUnderTest()testMethodUnderTest_whenInputIsNegative()testMethodUnderTest_resultIsInThePast()test prefix (considered redundant within the unit testing context): methodUnderTest(), methodUnderTest_whenInputIsNegative(), etc.onlyCamelCaseToCommunicatePreConditionsAndExpectedResults (without _ breaking up sections)The official oracle written style guide mentions:
String#substring, InputStream#readAllBytes.Packages are a bit special, because their naming convention often isn't followed as strictly as the rest of the naming conventions. Oracle says:
The prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com, edu, gov, mil, net, org, or one of the English two-letter codes identifying countries as specified in ISO Standard 3166, 1981.
Subsequent components of the package name vary according to an organization's own internal naming conventions. Such conventions might specify that certain directory name components be division, department, project, machine, or login names.
so, for example: com.abc.def would be a valid package name, while the commonly seen me.<name>.<project> would indicate that the company creating the package is located in montenegro. Some projects also just do not follow that recommendation outright, naming their packages simply <project>.
Sample answer (for some reason not copied automatically):