Java Naming Conventions
Java Naming Conventions
Java naming convention is a rule to follow as you decide what to name your identifiers such as class,
package, variable, constant, method, etc.
But, it is not forced to follow. So, it is known as convention not rule. These conventions are suggested by
several Java communities such as Sun Microsystems and Netscape.
All the classes, interfaces, packages, methods and fields of Java programming language are given
according to the Java naming convention. If you fail to follow these conventions, it may generate
confusion or erroneous code.
The following are the key rules that must be followed by every identifier:
Class
o It should start with the uppercase letter.
o It should be a noun such as Color, Button, System, Thread, etc.
o Use appropriate words, instead of acronyms.
o Example: -
Interface
o It should start with the uppercase letter.
o It should be an adjective such as Runnable, Remote, ActionListener.
o Use appropriate words, instead of acronyms.
o Example: -
1. interface Printable
2. {
3. //code snippet
4. }
Method
o It should start with lowercase letter.
o It should be a verb such as main(), print(), println().
o If the name contains multiple words, start it with a lowercase letter followed by an uppercase letter
such as actionPerformed().
o Example:-
1. class Employee
2. {
3. //method
4. void draw()
5. {
6. //code snippet
7. }
8. }
Variable
o It should start with a lowercase letter such as id, name.
o It should not start with the special characters like & (ampersand), $ (dollar), _ (underscore).
o If the name contains multiple words, start it with the lowercase letter followed by an uppercase
letter such as firstName, lastName.
o Avoid using one-character variables such as x, y, z.
o Example :-
1. class Employee
2. {
3. //variable
4. int id;
5. //code snippet
6. }
Package
o It should be a lowercase letter such as java, lang.
o If the name contains multiple words, it should be separated by dots (.) such as java.util, java.lang.
o Example :-
1. class Employee
2. {
3. //constant
4. static final int MIN_AGE = 18;
5. //code snippet
6. }
If the name is combined with two words, the second word will start with uppercase letter always such as
actionPerformed(), firstName, ActionEvent, ActionListener, etc.