Thursday, March 17, 2011

JAVA Programming : Naming Rules and Conventions

JAVA Naming Rules and Conventions

Before we start writing any Java Program or code we must know about some naming rules and convention of Java, so first we talk about some rules.

Java Naming Rules: Below are the set of rules that you must follow to write a error free code.

Java is Case Sensitive language that means that upper case latter and lower case latter are two different things. In  other words we can say that in Java there are 52 characters (26 upper and 26 lower case). For example JAVACODESPOT , JavaCodeSpot and javacodespot will be treated as different identifiers in Java.

First Character of any name can be any of these:
 a-z (any lower case alphabets)
A-Z (any lower case alphabets)
 $ (A dollar sign)
 _ (Underscore sign)
Note: the first character of any name can not be digit (0-9)
After first character a name can be anything that a first character can be and it can have a digit or number (0-9) also.

Java Naming Conventions: Bellow are the set of standards that are given by Sun that programmer should follow to make their code easier to read for themselves and for other programmers.

For Classes and Interfaces the first letter of the name should be capitalized and if the name is made up with several words, start of every word should be capitalized for example Dog, CamelCase.

For Methods and Variables the first word of the name should be lowercase and if the name is made up with several words, start of every word except first word should be capitalized for example dog, camelCase.

For Constant there is no predefined type in Java but you can have a constant by declaring variable static and final. A Constant name should be of uppercase and if the name is made up with several words the underscore should be used between the words for example DOG, CAMEL_CASE.

For Packages names should be in lowercase and full stop (.) as separator.

No comments:

Post a Comment