Home  >  Article  >  Java  >  Java Naming Conventions

Java Naming Conventions

PHPz
PHPzOriginal
2024-08-30 16:20:29846browse

In this article, we will be going through some naming conventions that should be followed, especially in Java for code maintainability and readability purposes, that help a programmer to understand and modify the code written by another programmer. We can consider it a guideline that one can follow while assigning names to one’s classes, variable or methods or interfaces, etc. and making it a good practice while writing codes. The naming conventions that we will discuss in this article are suggested and practiced by many Java programmers and supported by Netscape and Sun Microsystems as well.

Syntax in Java Naming Conventions

In Java programming language, the camel-case style of writing is used for writing names of methods/functions, variables and title-case style for classes and interfaces. Let’s go through and understand the rules that one should follow while naming an element/entity in Java Programming Language:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

  • While assigning names to classes, we should keep in mind that the class names should be a noun and start with a capital letter.

Syntax:

public class Employee { //code snippet }
  • In the case of interfaces, it should be ab adjective (that describes the noun (a class that implements it)) and begin with a capital letter. In some cases, an interface name can be a noun as well, when they represent a family of classes, ex: Map, List, etc.

Syntax:

interface Printable { //code snippet }
  • A method name should begin with a verb (defining an action that the method is going to implement) and start with a lower-case letter.

Syntax:

void printDetails() { //code snippet }
  • Whereas for variables, they should be meaningful, which signifies what kind of information it is storing, whether it is a name, an address, a phone number, etc. When writing names of variables, we need to make sure that it does not begin with special characters such as a dollar ($) or an underscore sign character (_) and its name should not have any whitespaces and begin with a lower-case letter.

Syntax:

int eid, sal;string firstName, lastName;
  • Names of constant variables should be full capitalized (all UPPERCASE) and separated with an underscore sign (_); its name consists of more than one word.

Syntax:

static final int MIN_SAL = 20000;
  • We know that Java uses all lower-case letters for assigning package names, and we should follow the same naming convention while naming our packages as well.

Syntax:

package src.employeedetails;
  • Enumerations in your java code should be capitalized, i.e. all upper-case letters.

Syntax:

enum JobPost { MANAGER, ANALYST, HR, ADMIN, ENGINEER }
  • Annotations in java can be a noun, verb or adjective and should follow title-case style for naming the annotations.

Syntax:

public @interface Documented {}

Examples in Java Naming Conventions

Now, let us write a full code using the above code snippets that makes it more meaningful and helps us understand why following naming conventions are important while writing application code in any programming language:

Example #1

Code:

package src.employeedetails;
interface Printable {
void printDetails();  }
public class Employee implements Printable {
int eid;
double sal;
String firstName, lastName;
// Default Constructor
Employee() {
eid=0; sal=0.0;
firstName = "Anonymous"; lastName = "Anonymous";
}
// Parameterized Constructor
Employee(int eid, double sal, String firstName, String lastName) {
this.eid = eid; this.sal = sal;
this.firstName = firstName; this.lastName  = lastName;
}
public void printDetails() {
System.out.println("Employee ID:" + eid + "\n" +
"Employee First Name:" + firstName + "\n" +
"Employee Last Name:" + lastName + "\n" +
"Employee Salary:" + sal + "\n" );
}
public static void main(String args[]) {
Employee emp = new Employee(1,22368.50,"Alan","Hope");
emp.printDetails();
}
}

Output:

Java Naming Conventions

Example #2

Code:

package src.customerdetails;
interface Printable { void printDetails(); }
public class Customer implements Printable {
int custid;
long mobNo;
String fullName,emailAddr;
// Default Constructor
Customer() {
custid=0; mobNo=0;
fullName = "Anonymous"; emailAddr = "[email protected]";
}
// Parameterized Constructor
Customer(int custid, long mobNo, String fullName, String   emailAddr) {
this.custid = custid; this.mobNo = mobNo;
this.fullName = fullName; this.emailAddr  = emailAddr;
}
public void printDetails() {
System.out.println("Customer ID:" + custid + "\n" +
"Customer Full Name:" + fullName + "\n" +
"Customer Email Address:" + emailAddr + "\n" +
"Customer Mobile Number:" + mobNo + "\n" );
}
public static void main(String args[]) {
Customer cust = new Customer (1,987451036,"Alan Hope","[email protected]");
cust.printDetails();
}
}

Output 1:

Java Naming Conventions

Output 2:

Java Naming Conventions

Advantages of Java Naming Conventions

Below are some advantages in java naming convention:

  • Reduction in writing confusion or erroneous code.
  • Improvement in code readability.
  • Less time spent to figure out what the code does.
  • Improvement in code maintainability.
  • Produce a consistent code throughout the application.

Conclusion

Next time while writing a Java code, make sure that the classes, interfaces, packages, methods, and fields you define and implement have names following the Java naming conventions. Remember those following naming conventions in any programming language is the first step to write clean and consistent code and is probably the first best practice that everyone programmer should follow.

The above is the detailed content of Java Naming Conventions. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Java App DevelopmentNext article:Java App Development