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.

CrossRef.eps For information about advanced forms of class declaration, see Abstract Class, Anonymous Class, extends Keyword, Final Class, and Inheritance.

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:

check.png 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.

check.png 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.

check.png 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.