Naming Conventions for Identifiers
If you have observed every program written by us closely, you would have noticed certain similarities in the way we have named our classes and variables. We always started our class name with a capital letter (HelloWorld, Addition). On the contrary, we have started a variable name with a small letter and then capitalised every subsequent word (firstNumber, secondNumber, result). These are the naming conventions which are followed by all. Following these conventions also makes a language simpler. If you remember, you have been said earlier while discussing about the case sensitivity of Java that there is no need to panic about what words are to be capitalised and what shouldn't be capitalised. You now need not spend time thinking if 's' in System should be capitalised or not. Since it is the name of a class, the 's' has a need to be stated in capitals. These conventions have been followed in the pre-defined classes of Java too. These classes together form the java API. We shall learn more about the PAI later on. For now, we shall see the conventions that are followed while naming classes, variables and methods.
The starting letter of each word in a class name is capitalised. All other letters are written in lower case. Examples: Addition, Car, HelloWorld
Method names follow a convention similar to that of class names except that the first letter is not capitalised. Till now, we have come across only one method- main. We shall shortly see how we can define as many methods as we want with any name that we wish. Examples of some method names are main, drawShape, printString, add.
Variables follow conventions similar to that of method names. However, when we use an identifier in a class, we can easily say if it is a method name or a variable name because of the parentheses that are placed adjacent to method name. Remember that when we had written result in the print() statement, we haven't appended a pair of parentheses at the end.
System.out.println ("Result:"+ result);
However, when we have called the print(), println() and printf() methods, we have a pair of parentheses at the end in which we have written the arguments. Not all methods require arguments. We shall study more about methods in detail in a later chapter.
In addition to classes, variables and methods, we also have packages, final variables and interfaces. The naming conventions for them will be stated when we come across them in our study.
Note that following conventions is not mandatory. You can name your class as 'helloWorld' or 'HellOWoRLd' and the program still works. But following the conventions makes your program looks professional and people going through your program wouldn't face any difficulties trying to comprehend whether a particular identifier is a variable, a class, an interface or some other entity.