Naming conventions in Java are a set of rules that apply to Java programs, and are defined in The Java Language Specification. They include recommendations for naming packages, classes, methods, and variables.
This shot discusses some basic naming conventions in Java.
PascalCasing is a naming convention where each word in a compound word is capitalized. "JavaNamingConventions" is an example of a PascalCased word in Java.
In camelCasing, the first word in a compound word is not capitalized. "javaNamingConventions" is an example of a camelCased word in Java.
In snake_casing, underscores separate the words in a compound word. "java_naming_conventions" is an example of a snake_cased word in Java.
Construct | Convention | Description | Examples |
Class | PascalCase | Classes should be named using PascalCasing, with the first letter of each word capitalized. A class name should be a noun or a word representative of an object. | MyClass, Employee |
Interface | PascalCase | Interfaces should be named using PascalCasing, with the first letter of each word capitalized. An interface name should be an adjective. | Serializable, Runnable |
Method | camelCase | Method should be named using camelCasing. Method names should always start with a lower case letter. | main(), print(), println() etc. |
Variable | camelCase | Variables should be named using camelCasing. Variable names should always start with a lower case letter. | num1, name, address |
Package | snake_case (all lower case) | Packages should be named using snake_casing. Package names should always start with a lower case letter. | java, lang, util |
Constant | snake_case (ALL UPPER CASE) | Constant variables should be named using snake_casing. Contant variable names should have all upper case letters. | MAX_VALUE, MIN_VALUE etc. |
Below is a code example demonstrating the usage of Java naming conventions.
package io.educative.example;public class Employee{public static final int MAX_AGE=100; //Constant variable naming conventionprivate String empName; //Variable naming conventionprivate int age;public Employee(String name, int age){ //Constructor naming conventionthis.empName=name;this.age=age;}public void setName(String name){ //Method naming conventionthis.empName = name;}public int getAge(){ //Method naming conventionreturn age;}public static void main(String args){ //Main method naming conventionEmployee emp1 = new Employee("John Doe", 30);System.out.println("Employee Name: "+emp1.empName); //Prints Employee NameSystem.out.println("Employee Age: "+emp1.getAge()); //Prints Employee Age}}
In the above code example, we follow different naming conventions for different parts of the code. We use PascalCasing for class and interface names, camelCasing for method and variable names, and snake_casing for constant variable names. We also use lower case letters for package names.
Following the Java naming conventions makes our code more readable and consistent. It is not mandatory to follow these conventions, but it is generally a good idea to do so.