Java Naming Restrictions
When developers create applications in the Java language, they can choose names for various code elements, including variables, classes, methods and packages. For the most part developers have a free choice when making these decisions, but there are some restrictions and conventions within the language that most development projects observe. In general, developers taking a best practice approach try to use meaningful names and to observe the recognized naming guidelines for Java programming, as this creates applications that are easier to work with.
-
Variables
-
In a Java program, you can give a variable any name beginning with a letter, a dollar sign or an underscore character. Anything other than this is not legal and will prevent a Java program from compiling. If you program in an Integrated Development Environment you will see error messages if you attempt to use a variable name beginning with another character. According to Java convention, it is standard to begin a variable name with a lower case letter unless it is a constant. The name will normally comprise a meaningful representation of the variable purpose, sometimes with multiple sections, each section after the first starting with an upper case letter, as in the following example:
String firstName;
Methods
-
Methods in Java programs must observe the same rules as variables in terms of legal characters. It is also standard to begin a method name with a lower case letter, with meaningful sections beginning with an upper case character. While variables often use names involving nouns, methods are often most meaningful when they use verbs, since a method is a process executed by the program. For example, a method to return the "firstName" variable could appear as follows:
public String getFirstName()Teams of developers often work together on large projects, so using meaningful names can help developers to re-use one another's code.
-
Classes
-
When Java developers create class declarations, the standard approach is to use noun-based names, representing the role objects of the class will play within the application. It is also standard to begin class names with upper case letters, splitting the names into multiple parts if necessary, each section starting with another upper case letter. For example, in an application handling employee pay, a class could be dedicated to carrying out tax calculations, with the following class declaration outline:
public class TaxCalculator
Packages
-
Java applications group classes into packages. These packages wrap well-defined sections of application functionality, with the classes inside a package often working in conjunction with one another to deliver a specific area of program responsibility. Package names often have multiple elements in them, as in the following example:
java.ioThis refers to the input and output classes within the Java language itself. Development teams can use multiple internal categories within their package names, but the convention is to use all lower case letters.
-
References
Resources
- Photo Credit Comstock/Comstock/Getty Images