Naming Your Variables

Variable names in Java can begin with a letter, underscore character (_), or a dollar sign ($). The rest of the name can be any letters or numbers. You can give your variables any names you like but should be consistent in how you name variables. This section outlines the generally recommended naming method for variables.

Java is case-sensitive when it comes to variable names, so you must always capitalize variable names the same way. For example, if the gameOver variable is referred to as GameOver somewhere in the program, an error prevents the program from being compiled.

A variable’s name should describe its purpose in some way. The first letter should be lowercase, and if the variable name has more than one word, make ...

Get Sams Teach Yourself Java™ in 24 Hours, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.