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.
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
Syntax:
public class Employee { //code snippet }
Syntax:
interface Printable { //code snippet }
Syntax:
void printDetails() { //code snippet }
Syntax:
int eid, sal;string firstName, lastName;
Syntax:
static final int MIN_SAL = 20000;
Syntax:
package src.employeedetails;
Syntax:
enum JobPost { MANAGER, ANALYST, HR, ADMIN, ENGINEER }
Syntax:
public @interface Documented {}
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:
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:
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:
Output 2:
Below are some advantages in java naming convention:
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!