Pages

Naming Convention in Java

Naming Convention


Name?

In our daily life Name plays a very important role to identify the Object, i.e- name, place, thing, etc.
Just think for a while if the world existing without any name.
Think once what would happen if there is no name of yourself, no name of your family person, your relative, your friends, etc.
A world where everything existed without a name, country without name, city without name, district without a name, etc?

Literally, the "Name" is very important.

    

about the naming convention:-

As much as name is important to us, the same as a name with appropriate meaning is also very important to us.
The naming convention is used to define the name with appropriate meaning.

Most of the time on the basis of a name you can assume the object, and its behaviour, i.e- is it any country name or city or a person, a person is male or female etc. 
So the naming convention is very important in every situation.

the naming convention in Java:-

Since a name is important in real life, the same as the name is important in programming life, to identify the Package, class, method, variable, interface, etc.

A code with appropriate naming is much understandable by the developer.
Naming Convention

Need for the naming convention:-

Note- Remember the thing that, These are convention, not rules, they are not compulsory to follow, but it's good to follow.
  1. A software project development is a long term process, so it will never possible that only you will manage that project lifetime, the developers will change time to time for sure, so it is the responsibility of the previous developer that the deliverable code is well understandable by the newly hired developer. At this point, naming plays a key role to understand the code.
  2. it is recommended by a high-level developers community to follow the naming convention.
  3. it increases the readability of the program.
Where we have to follow naming convention in programming:-

We have to follow the naming convention on bellow point--

package name:

the package name must start with a lowercase character, no capitalization in the package name, 
no extra character will be used, yes you can use version name at last if required. 

com.example.projectname
com.example.projectname.v1
com.example.projectname.anotherpackagename

and if you have a company domain used it with the reverse of the domain.

com.javaoneword.firstdemo
com.sun.eng

Class name:

The class name always starts with Capitalized character Thread, String, Student, etc, and we can say that a class is always a noun.
If your class name is mixed of more that one word, use the first character of each word is capitalized ArithmeticException, StringBuilder, StringBuffer, HasMap.

String, Thread, Customer
StringBuilder, StringBuffer, ArithmeticException, etc

Interface name:

Interface name should decide the same as for Class name, the interface is an adjective, sometimes it can be noun also like, Map, List.

Runnable, List, Map, etc

method name:

The method name should be a verb. method name should start with a lowercase character, as the first letter of the method is in lowercase, and if the word is mixed after the first word else word is started with a capitalized word.

run(), addNumber() etc

Variables name:

variables and fields are also should be a noun, it should be short and meaningful.
 all instance (object of class), class, and class constants are in mixed case with a lowercase first letter, and after that every word start with capitalized char, Variable names should not start with underscore "_" or dollar sign" $ " characters, they both are allowed but do not use these two also.
Do not use a single-character name as a variable or field name.
if there is need of any temporary variable i.e. for loop, then go for single-character name,
use i, j, k, m for int type variable and c, d, e for character-related functionality.

int i;
char c;
String userName;
String userEmail;
Student student; etc

Constant:

If you are going to define any constant, strictly follow the naming convention.
Constant should be always in upper case and another word is separated by "_".


static final int REQUEST_ID=1001;
stati final int MIN_WIDTH = 20;

4 comments:

  1. Good job sir....its really a helpful content for us.

    ReplyDelete
  2. Thanks to like the post, visit the website, surely you'll find some more helpful content.

    ReplyDelete
  3. This is very usefull, thank you!

    ReplyDelete