Showing posts with label Code Conventions in Java. Java Code Conventions.. Show all posts
Showing posts with label Code Conventions in Java. Java Code Conventions.. Show all posts

Saturday, 1 August 2015

Naming conventions in java

Java Naming Conventions


What Is a Naming Convention?


          Naming Conventions is nothing but a standard specifically used by the Java professional to represent the name of the class, interface, method, variable, package etc in a Java Program. In other words, Naming Conventions in Java is a standard rule to follow in selecting name of any identifiers in a program.

Naming Convention


          It is not mandatory to follow the Naming Conventions to run a java program. That’s why it is known as convention and not made as a rule. 


Standard Java Naming Conventions


          Standard Java Naming conventions for different identifiers are listed below


Package


          Package names should be in all lowercase letters (small letters). 

Eg: com.packet.example, crack.analyser, name.preview


Class


          Typical class name should be in nouns and represented by using the first letter capital method (Upper Camel Case Method). Use simple and descriptive nouns as Class name.  

Eg: class LogicalMeter, class Calculator, Class SeperateString


Method


          Method name should be verb and use mixed case letters to represent it in programs. Mixed case letters are also knows as lower camel case letters. 

Eg: toPrint(), colorBackground()


Constant


          Constants are declared with the help of ‘static’ and ‘final’ keyword in Java. Constant variables should declare with full Upper Case letters with underscore(_) for word separation.

Eg: static final int MAX_WIDTH, static final int MAX_HEIGHT


Interface


          Interface name is also named like class name with Upper Camel Case method. 

Eg: interface ActionListener, interface Runnable


Variable


         Variable name should be in mixed case ie, naming start with small letter and use capital for internal words. It is easier to use simple one letter variable for temporary usage. Common variables used for storing integers are I, j, k and m. Common variables used for characters are c, d and e.

Eg:  float myHeight, String userName


Advantage of naming conventions in java


          Naming conventions make programs more understandable by making them easier to read. They can also give information about the function of the identifier-for example, whether it's a constant, package, or class-which can be helpful in understanding the code.