Putting One Class inside Another

Although a Java program is sometimes called a class, there are many occasions when a program requires more than one class to get its work done. A multiclass program consists of a main class and any helper classes that are needed. These helper classes earn their name by helping the main class do its work.

An example might be a Java applet that displays a scrolling headline as part of its graphical user interface. The headline could be an independent object in the program, just like other interface elements such as buttons and scroll bars. It makes sense to put the headline into its own class, rather than including its variables and methods in the applet class.

When you divide a program into multiple classes, there ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH 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.