Sunday, August 18, 2013

Java Naming Standards...

Rules


  • Identifiers (variables, classes, methods, packages and interface) must be composed of letters, numbers, underscore ( _ ) or dollar sign ( $ ).
  • Identifiers must begin with letter, underscore or dollar sign.
  • Keywords cannot be used as identifiers.
  • Spaces are not allowed between the words.
  • Variables can have any length.

  Ex: apple, _apple, $apple, Apple123, _123apple

  • There are examples for illegal identifiers.
  Ex: 123mango, Mango#, Fresh mango, _1@mango

Java key words


abstract
boolean
break
byte
case
catch
char
class
const
continue
default
do
double
else
extends
final
finally
float
for
goto
if
implements
import
instanceof
int
interface
long
native
new
package
private
protected
public
return
short
static
strictfp
super
switch
synchronized
this
throw
throws
transient
try
void
volatile
while
assert
enum







Classes and interfaces naming standards


  • The first letter should be capitalized and camel case is allowed. 
  • Class names should be nouns.
      • Ex: Apple, MyMobile 
  • Interfaces names should be adjectives.
      • Ex: Readable 

Methods, variables and constants naming standards 

  • Method names and variable names should be started with lowercase and other words should be in Upper case (camel case). 
  • Method names typically be verb-noun pairs.
      • Examples for methods: doCalculation( ), getName( )
      • Examples for variables: name, userName 

  • All the letters should be in uppercase in constants. Other words should be separated by underscore. 
                Ex: MAX_LENGTH , TOP_LEVEL 

JavaBeans Standards 


JavaBeans property naming rules
  • If the property is not a Boolean, then getter method's prefix must be ‘get’.
                Ex: getSize( ), getName( ) 
  • If the property is Boolean, the getter method’s prefix is either ‘get’ or ‘is’.
                Ex: getStopped( ), isStopped( ) 
  • The setter method’s prefix must be ‘set’.
                Ex: setSize( ) 

  • Setter method signatures must be marked public with a void return type and an argument that represents the property type. 
  • Getter method signatures must be marked public that take no arguments and have a return type that matches the argument type of the setter method for that property. 

JavaBean Listener naming rules
  • Listener method names used to “register” a listener with an event source must be the prefix ‘add’ followed by the listener type.
      • Ex: addActionListener ( ) 
  • Listener method names used to remove (“register”) a listener must use the prefix remove, followed by the listener type. 
  • Listener method names must be end with the word “Listener”. 

Comments in java 


//this is single line comment 


/* Multi line comment 
 * Multi line comment 
 * Multi line comment 
 */


/** 
 *Documentation comment 
 * 
 */