Naming Conventions in Java (Class, Method, Package)

.
Java Naming conventions


Java Naming Convention For Classes
  • Class names start with an upper case letter and every inner word starts with an upper case letter.

  • This convention is also known as the camel case convention.

  • The class name should be nouns.

Example

The examples of Java naming conventions for classes are listed below.

String, StringBuffer, InputStreamReader ……etc


public class Emp

//Business Logic

}  

Java Naming Convention For Interface

  • Interfaces start in upper case and every inner word starts with an upper case letter.

Example
 An example of the Java naming convention for Interfaces is listed below. 

Serializable Cloneable RandomAccess…etc


interface Printable 


//Business Logic

}  



Java Naming Convention For Enum

  • Enum starts with an upper case letter and every inner word starts with a capital letter.
Example 

Enum, ElementType, RetentionPolicy…etc

Java Naming Convention For Annotation

  • Annotation starts with an upper case letter and every inner word starts with a capital letter.
Example 
Override, FunctionalInterface …etc

Java Naming Convention For Method

  •  Method name starts with lower case letter and every inner word starts with upper case letter.
  •  This convention is also known as the mixed case convention
  • The method name should be verbs.
Example
 post(), charAt() ,toUpperCase()


class Employee 


void draw() //method 

//Business Logic


}  


Java Naming Convention For Variable

  • Variables start with lower case letters and every inner word begins with upper case letters.
Example 
 out in pageContext …etc


class Employee
 

int id;  //Variable

//Business Logic

}  


Java Naming Convention For Packages

  • The package name is always must written in lowercase letters.
Example:- 
java. lang java.util java.io …etc


package com.javatpoint;   //package 
class Employee 


//Business Logic

}


Java Naming Convention For Keywords

  • Keywords While declaring keywords every character should be lowercase.
Example 
try, if, break, continue…..etc


Java Naming Convention For Constants

  • Constants:-While declaring constants all the words are uppercase letters.
Example: MAX_PRIORITY MIN_PRIORITY NORM_PRIORITY


class Employee 

 static final int MIN_AGE = 18; //constant 
 
//Business Logic

}  

NOTE
The coding standards are mandatory for predefined libraries & optional for user-defined libraries But as a Java developer, it is recommended to follow the coding standards for user-defined libraries also.






Post a Comment

0 Comments