Java naming convention classes

Java Naming Conventions

Java naming conventions are sort of guidelines that application programmers are expected to follow to produce consistent and readable code throughout the application. If teams do not follow these conventions, they may collectively write an application code that is hard to read and difficult to understand.

Java heavily uses Camel Case notations for naming the methods, variables etc. and TitleCase notations for classes and interfaces.

Let’s understand these naming conventions in detail with examples.

Package names must be a group of words starting with all lowercase domain names (e.g. com, org, net, etc). Subsequent parts of the package name may be different according to an organization’s own internal naming conventions.

package com.howtodoinjava.webapp.controller; package com.company.myapplication.web.controller; package com.google.search.common;

In Java, class names generally should be nouns, in title-case with the first letter of each separate word capitalized. e.g.

public class ArrayList <> public class Employee <> public class Record <> public class Identity <>

In Java, interfaces names, generally, should be adjectives. Interfaces should be in the title case with the first letter of each separate word capitalized. In some cases, interfaces can be nouns as well when they present a family of classes e.g. List and Map .

public interface Serializable <> public interface Clonable <> public interface Iterable <> public interface List <>

Methods always should be verbs. They represent action and the method name should clearly state the action they perform. The method name can be single or 2-3 words as needed to clearly represent the action. Words should be in camel case notation.

public Long getId() <> public void remove(Object o) <> public Object update(Object o) <> public Report getReportById(Long id) <> public Report getReportByName(String name) <>

All instance, static and method parameter variable names should be in camel case notation. They should be short and enough to describe their purpose. Temporary variables can be a single character e.g. the counter in the loops.

public Long id; public EmployeeDao employeeDao; private Properties properties; for (int i = 0; i

6. Constant Naming Conventions

Читайте также:  Learn java and python

Java constants should be all UPPERCASE where words are separated by underscore character (“_”). Make sure to use the final modifier with constant variables.

public final String SECURITY_TOKEN = ". "; public final int INITIAL_SIZE = 16; public final Integer MAX_SIZE = Integer.MAX;

Generic type parameter names should be uppercase single letters. The letter ‘T’ for type is typically recommended. In JDK classes, E is used for collection elements, S is used for service loaders, and K and V are used for map keys and values.

public interface Map <> public interface List extends Collection <> Iterator iterator() <>

Similar to class constants, enumeration names should be all uppercase letters.

Annotation names follow title case notation. They can be adjectives, verbs, or nouns based on the requirements.

public @interface FunctionalInterface <> public @interface Deprecated <> public @interface Documented <> public @Async Documented

In this post, we discussed the naming conventions in Java to be followed for consistent writing of code which makes the code more readable and maintainable.

Naming conventions are probably the first best practice to follow while writing clean code in any programming language.

Источник

Java Naming Conventions

Java Naming Conventions

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.

Web development, programming languages, Software testing & others

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:

  • While assigning names to classes, we should keep in mind that the class names should be a noun and start with a capital letter.
  • 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.
  • 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.
  • 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.
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.
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.
package src.employeedetails;
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

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(); >>

naming convention1

Example #2

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 /cdn-cgi/l/email-protection" data-cfemail="62230c0d0c1b0f0d1711220d1203134c010d0f">[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(); > >

Java Naming Conventions 2

Java Naming Conventions 3

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.

This is a guide to Java Naming Conventions. Here we discuss the classes, interfaces, packages, methods, and fields that define and implement the Java naming conventions. You can also go through our other related articles to learn more –

500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access

1000+ Hours of HD Videos
43 Learning Paths
250+ Courses
Verifiable Certificate of Completion
Lifetime Access

1500+ Hour of HD Videos
80 Learning Paths
360+ Courses
Verifiable Certificate of Completion
Lifetime Access

3000+ Hours of HD Videos
149 Learning Paths
600+ Courses
Verifiable Certificate of Completion
Lifetime Access

All in One Software Development Bundle 3000+ Hours of HD Videos | 149 Learning Paths | 600+ Courses | Verifiable Certificate of Completion | Lifetime Access

Financial Analyst Masters Training Program 1000+ Hours of HD Videos | 43 Learning Paths | 250+ Courses | Verifiable Certificate of Completion | Lifetime Access

Источник

Оцените статью