If you have observed any program written by us closely, you would have noticed some similarities in the way we have named our classes and variables. We have always started our class names with a capital letter (HelloWorld, Addition). On the contrary, we have started our variable names with a lower case letter and then capitalized each subsequent word (firstNumber, secondNumber, result). This is a standardized naming convention that all programmers follow. Following this convention also makes the language simpler. If you remember, you had said earlier when discussing about case sensitivity in java, that there is no need to worry about which words should be capitalized and which should not be capitalized. Now you do not have to spend time thinking whether the ‘s’ in System should be capitalized or not. Since it is a class name, the ‘s’ should be capitalized. These conventions have been followed in the predefined Java classes as well. These classes together form the java API. We will learn more about API later. For now, we will look at the conventions followed while naming classes, variables and methods/functions.
The first letter of each word in a class name is capitalized, followed by a lowercase letter. Examples: Addition, Car, HelloWorld
Method/function names follow a similar convention to class names except that the first letter is not capitalized. So far, we have only encountered one method—main. We will soon see how we can define as many methods as we want with whatever names we want. Examples of some method names are main, drawShape, printString, add.
Variables follow a similar convention to method names. However, when we use identifiers in a class, we can easily tell whether it is a method name or a variable name because of the parentheses placed adjacent to the method name. Remember that when we wrote the result in the print() statement, we did not add a pair of parentheses at the end.
System.out.println ("Result:"+ result);
However, when we have called print(), println() and printf() methods, we have a pair of parentheses at the end where we have written the arguments. Not all methods require arguments. We will learn more about methods in detail in the next chapter.
In addition to classes, variables & methods, we also have packages, final variables & interfaces. The naming conventions for them will be stated as we encounter them in our research.
Note that the following conventions are not mandatory. You can name your class as 'helloWorld' or 'HellOWoRLd' and the program will still work. But following the conventions makes your program look professional and people going through your program will not have trouble trying to understand whether a particular identifier is a variable, a class, an interface or some other entity.