Monday, June 1, 2020

Naming Conventions in Java

Naming Conventions: We need to follow java naming conventions while writing java programs . In java we are recommended to follow the naming conventions while defining packages,classes,methods ,variables Interfaces and constants for better maintenance and readability of code.


i)For Packages: In java the prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, like com, edu, gov, mil, net, org. Subsequent components of the package name vary according to an organisation’s own internal naming conventions.

Example:
                java.io
                java.lang
                java.util
                com.smartjavaguru

ii)For Classes and Interfaces: While defining classes and Interfaces we need to make sure the first letter starts with a Capital letter of the word or phrase of the name .The name may consists of combination of words in that case first letter of each word starts with a capital letter.

  • Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Interfaces name should also be capitalized just like class names.
  • Use whole words and must avoid acronyms and abbreviations.

Example :  interface Vehicle {}

                    interface Sport{}

                    interface Bike{}

                   class Jeep implements Vehicle{ },

                   class Football implements Sport{}

                  class MoutainBike{}


iii)For Methods : In java all methods should be verbs, all method names starts with a lower case letter (first word first letter) and if the method name consists of more than one word then from the second word onward each word starts with a capital letter (Camel case)

Examples:

                    void sumOfTwoNums(int num1,int num2);

                    void speed(int increment);

                    void calculateInterest(int interestRate);

iv)Variables : In java variable names should be short and meaningful.Variable names should not start with underscore ('_') or dollar sign '$' chars. Single char names should be avoided except for temporary variables .All the variable names should be camelCase letters.

Example: 

                    int num=10;

                    int rate=2;

                    int speed=100;

v)Constants: In java all constants names should be all uppercase with words separated by underscores (“_”). There are various constants used in predefined classes like Float, Long, String etc.

Example: 

               int MIN_LENGTH=10;

              int MAX_SPEED=120;




No comments:

Post a Comment

Hello Buddy, if you have any doubts or need any clarification , feel free to comment. Thanks.