Naming conventions in java
There are certain rules which you need to follow in java while naming your identifiers such as class, package, variable, constant, method, etc. These Rules are commonly known as Naming Convention.
But these Naming Conventions are not forced to follow but these are suggested by several Java communities such as Sun Microsystems and Netscape.
So "Why to follow these Naming Conventions ?" you may ask
The simplest answer to this question is that " All the classes, interfaces, packages, methods and fields of Java programming language are given according to the Java naming convention and if you fail to follow these conventions, it may generate confusion or erroneous code."
So long story short It's better to be Safe than Sorry 😉
Now let's start with exploring different naming convention in java.
Rule 1
Name should not match with keywords.
Rule 2
For constant every letter should be a capital
Rule 3
- While naming a Class " The class should be a Noun "
e.g : Student , Person
- While naming a Class with single word " The name of that class should begin with Capital Letter "
e.g : Technotoken
- While naming a Class with multiple words " The first letter of every word of that class should begin with Capital Letter "
e.g : ShareNow
Note : The naming of names with first letter being a Capital letter is known as Camel Casing Rule. Also except for Packages and Constants , Camel Casting Rule is used everywhere in java.
Rule 4
- While naming a method the method should be a Verb.
e.g : read ( ), run ()
- While naming a method the first letter should begin with lowercase letter.
e.g : read ( )
- while naming a method with multiple words " The first letter of first word should be small ( lowercase ) and first letter of every consecutive word should be a Capital Letter "
e.g : getName ( )
Rule 5
- While naming a Variable the variable should begin with alphabet or underscore.
e.g : roll_number , _starting_with_under_score_
- It should not begin with a digit.
e.g : 3idiots
- Should contain alphanumeric but not symbols or spaces except underscore.
e.g : ABC345
Rule 6
- While naming an Interface the interface should be an adjective.
e.g : Runable , Readable , Remote
Advantages
- Code is more Readable.
- Code is more Efficient.
- Code looks Good.
No comments
If you have any doubts, please let us Know.