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
Eric McIntyre
Eric McIntyre7mo ago
Package names should generally be lowercase.
package mypackage
package mypackage
Larger projects may need to separate packages more granularly.
package services.dj45x.mypackage
package services.dj45x.mypackage
Classes are most commonly CamelCased.
class CustomerModel
class CustomerModel
Same applies to interfaces
interface EnumerableTypes
interface EnumerableTypes
Variables should use mixedCase.
String getName()
String getName()
Constants should be uppercase
static final int DEFAULT_WIDTH
static final int DEFAULT_WIDTH
Submission from dj45x
Eric McIntyre
Eric McIntyre7mo ago
variables lower case methods camelcase constants upper case classes first letter uppercase methods are verbs
Eric McIntyre
Eric McIntyre7mo ago
classes and variables are nouns
Submission from gonzothegovernment
Eric McIntyre
Eric McIntyre7mo ago
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
Eric McIntyre
Eric McIntyre7mo ago
Do you mean UpperCamelCase for class names and lowerCamelCase for most other variables and identifiers?
Submission from tofu_ninja1
Eric McIntyre
Eric McIntyre7mo ago
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
Eric McIntyre
Eric McIntyre7mo ago
Idk if I understand it the right way but there are Getter/Setter Methods that are named by "Get..." Or "Set..."
Submission from kampfmietze
Eric McIntyre
Eric McIntyre7mo ago
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
Eric McIntyre
Eric McIntyre7mo ago
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
Eric McIntyre
Eric McIntyre7mo ago
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.
Eric McIntyre
Eric McIntyre7mo ago
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.
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>.
⭐ Submission from 0x150
dan1st
dan1st7mo ago
Sample answer (for some reason not copied automatically):
Eric McIntyre
Eric McIntyre7mo ago
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.
package com.mydomain.myproject.somesubpackage;

public class ClassNamesArePascalCase{
private static final String CONSTANTS_ARE_UPPER_CASE = "Hello World";
private int variablesAreCamelCase = 13;

public void methodNamesAreCamelCase(){
int localVariablesAreAlsoCamelCase = 37;
}
}
package com.mydomain.myproject.somesubpackage;

public class ClassNamesArePascalCase{
private static final String CONSTANTS_ARE_UPPER_CASE = "Hello World";
private int variablesAreCamelCase = 13;

public void methodNamesAreCamelCase(){
int localVariablesAreAlsoCamelCase = 37;
}
}
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.
Want results from more Discord servers?
Add your server