Week 74 — What are common naming conventions used in Java programs?
Question of the Week #74
What are common naming conventions used in Java programs?
13 Replies
Package
names should generally be lowercase.
Larger projects may need to separate packages more granularly.
Classes
are most commonly CamelCased.
Same applies to interfaces
Variables
should use mixedCase.
Constants
should be uppercase
Submission from dj45x
variables lower case
methods camelcase
constants upper case
classes first letter uppercase
methods are verbs
classes and variables are nouns
Submission from gonzothegovernment
In Java we have common naming conventions like this:
- names of variables: start with a lowercase letter and continue with camel case. E.g: userGivenInput
- names of constant variables: names of constant variables are written in uppercase only and use underscore instead of space. E.g: MAX_VALUE
- names of Classes: start with an Uppercase letter and continue with camel case. E.g: UserProfileClass
- names of methods: start with a lowercase letter and continue with camel case. E.g: getUserAge()
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()
Submission from giotsche
Do you mean UpperCamelCase for class names and lowerCamelCase for most other variables and identifiers?
Submission from tofu_ninja1
There are 3 naming conventions used in Java programs:
- camelCase is used to describe most variables and methods. The only exception to this rule are variables that are both static and final.
- SCREAMING_SNAKE_CASE is used to describe variables that are both static and final.
- PascalCase is used to describe classes, interfaces, enums, and annotations.
Submission from codewriter3000
Idk if I understand it the right way but there are Getter/Setter Methods that are named by "Get..." Or "Set..."
Submission from kampfmietze
if the answers are to be submitted here
the 1st is most famous camel case
1. Lower Camel Case -
it works just like a camels back
the first alphabet is small and the next main name(i cant recall whats its called) is upper
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
3. snake case
it is used while addressing a package name
example = com.example.controller , com.example.entity
for now i can only recognise this
thankyou
Submission from nova.imperator
Universal/official conventions:
- Types (classes, interfaces, records, etc.):
UpperCamelCase
- Static final fields (aka "constants"): UPPER_SNAKE_CASE
- Methods, instance fields, local variables: lowerCamelCase
- Packages: generally dot.separated.lower.case.components
, though sometimes camelCase
components are used
All of the above identifiers permit digits, but not as the first character (compiler-enforced)
Others:
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:
- Prefix of "test", followed by the name of the method or behavior being tested: testMethodUnderTest()
- Prefix of "test", followed by the name of the method or behavior being tested, followed by some pre-condition or invariant: testMethodUnderTest_whenInputIsNegative()
- Prefix of "test", followed by the name of the method or behavior being tested, followed by the expected result: testMethodUnderTest_resultIsInThePast()
- Any of the above, but dropping the test
prefix (considered redundant within the unit testing context): methodUnderTest()
, methodUnderTest_whenInputIsNegative()
, etc.
- Any of the above, but using onlyCamelCaseToCommunicatePreConditionsAndExpectedResults
(without _
breaking up sections)
... And so on. Really, more flexibility is acceptable in unit tests, due to the aforementioned information that needs to be conveyed. The important thing here is consistency.⭐ Submission from dangerously_casual
The official oracle written style guide mentions:
- PascalCase to be used for class names, interfaces, etc. Everything that represents a type (apart from the primitives) should be written in PascalCase: String, ByteBuf, StringBuilder, InputStream, etc
- camelCase to be used for everything else: methods, fields, locals.
String#substring
, InputStream#readAllBytes
.
- UPPER_SNAKE_CASE to be used for constants, so static final fields.Packages are a bit special, because their naming convention often isn't followed as strictly as the rest of the naming conventions. Oracle says:
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>
.⭐ Submission from 0x150
Sample answer (for some reason not copied automatically):
Naming conventions ensure that projects have similar names and allow to distinguish e.g. packages, classes and methods/variables. For that, it is important developers don't mix naming conventions.
Most Java code follows these naming conventions:
- Type names (e.g. classes or interfaces) should be PascalCase meaning the first letter of every word is written in uppercase while all other letters are lowercase.
- Constants are ALL_UPPERCASE_SEPARATED_BY_UNDERSCORES.
- Other variables (both fields and local variables) as well as methods are camelCase. This means the identifiers start with a lowercase letter, all subsequent words start with an uppercase letter and all other letters are lowercase.
- Package names are all_lowercase whereas dots are used to create a hierarchy of packages. All packages of an application are typically grouped together in some top level package. This typically uses reverse domain name notation. For example, an application called
MyApplication
maintained by a company/individual owning the domain example.com
would put all its classes in a package named com.example.myapplication
or subpackages.
There are other naming conventions as well (for example prepending getters with get
and setters with set
, e.g. getMyVariable
/setMyVariable
.
Aside from that, projects may have specific naming conventions.