Class Declaration
A class is defined by a class declaration, which follows this basic form:
[public] class ClassName {class-body}
The public
keyword indicates that this class is available for use by other classes. Although it’s optional, you usually include it in your class declarations so that other classes can create objects from the class you’re defining.
The ClassName
provides the name for the class. You can use any identifier you want to name a class, but the following three guidelines can simplify your life:
Begin the class name with a capital letter. If the class name consists of more than one word, capitalize each word: for example, Ball
, RetailCustomer
, and GuessingGame
.
Whenever possible, use nouns for your class names. Classes create objects, and nouns are the words you use to identify objects. Thus, most class names should be nouns.
Avoid using the name of a Java API class. No rule says that you absolutely have to, but if you create ...
Get Java For Dummies Quick Reference 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.