Java Quick Tip: Naming ConventionsPosted by RoboDuke on Monday, 26 November 2007
So, naming in Java...
Unlike many computer languages, Java programmers do use rules for naming items. You will see that I carefully follow these rules. In a nutshell, make names meaningful and usually use only letters, no spaces, no hyphens. Name:
- Classes - capitalized each word in the class name
- Valid - Dog, MyFirstProgram, AccountDriver
- Invalid - dog, myFirstProgram, accountdriver
- Instance variables - capitalized each word except the first one. Should be a noun
- Valid - breed, firstName, countryOfOrigin
- Invalid - Breed, FirstName, country of origin
- Methods - capitalize each word except the first one AND always end with ( ). Should be a verb
- Valid - bark(), calculateInterest(), changeDate()
- Invalid - bark, CalculateInterest(), changedata()
You might ask "why do I care?" The simple answer is that you can take a Java program that you have never seen before, and quickly figure out what items in it are classes, instance variables, and methods. Really helps you get up to speed on the code quickly!