5.1. What Is a Class?

As you saw in Chapter 1, a class is a prescription for a particular kind of object—it defines a new type. You use the definition of a class to create objects of that class type—that is, to create objects that incorporate all the components specified as belonging to that class.

In case that's too abstract, look back to the previous chapter where you used the String class. The String class is a comprehensive definition for a String object, with all the operations you are likely to need specified. Whenever you create a new String object, you are creating an object with all the characteristics and operations specified by the class definition. Every String object has all the methods that the String class defines built in. This makes String objects indispensable, and string handling within a program easy.

The String class lies toward one end of a spectrum in terms of complexity in a class. The String class is intended to be usable in any program. It includes facilities and capabilities for operating on String objects to cover virtually all circumstances in which you are likely to use strings. In most cases your own classes won't need to be this elaborate. You will typically be defining a class to suit your particular application, and you will make it as simple or complex as necessary. Some classes, such as a Plane or a Person, for example, may well represent objects that can potentially be very complicated, but the application requirements may be very limited. ...

Get Ivor Horton's Beginning Java™ 2, JDK™ 5th 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.