Putting One Class Inside Another
Although a Java program is called a class, there are many occasions when a program requires more than one class to get its work done. These programs consist of a main class and any helper classes that are needed.
When you divide a program into multiple classes, there are two ways to define the helper classes. One way is to define each class separately, as in the following example:
public class Wrecker { String author = "Ignoto"; public void infectFile() { VirusCode vic = new VirusCode(1024); }}class VirusCode { int vSize; VirusCode(int size) { vSize = size; }}
Caution
If more than one class is defined in the same source file, only one of the classes can be public
. The other classes ...
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.