#Week 74 — What are common naming conventions used in Java programs?

17 messages · Page 1 of 1 (latest)

spare canyonBOT
#
Question of the Week #74

What are common naming conventions used in Java programs?

oblique scaffoldBOT
#

Package

names should generally be lowercase.

package mypackage

Larger projects may need to separate packages more granularly.

package services.dj45x.mypackage

Classes

are most commonly CamelCased.

class CustomerModel

Same applies to interfaces

interface EnumerableTypes

Variables

should use mixedCase.

String getName()

Constants

should be uppercase

static final int DEFAULT_WIDTH
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

  1. 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:

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
fickle shard
#

Sample answer (for some reason not copied automatically):