Putting One Class inside Another





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 are two ways to define the helper classes. One way is to define each class separately, as in the following example:


public class WreakHavoc {

    String author = "Ignoto";



    public void infectFile() {

        VirusCode vic = new VirusCode(1024);

    }

}



class VirusCode {

    int vSize;



    VirusCode(int size) {

        vSize = size;

    }

}


In this example, the VirusCode class is being used as a helper of the WreakHavoc class. Helper classes will often be defined in the same .java source file as the class they're assisting. When the source file is compiled, multiple class files will be produced. The preceding example would produce the files WreakHavoc.class and VirusCode.class.

Watch Out!

If more than one class is defined in the same source file, only one of the classes can be public. The other classes should not have public in their class statements. Also, the name of the source file should match the name of the public class. In the preceding example, the name should be WreakHavoc.java.


When creating a main class and a helper class, you can also put the helper inside the main class. When this is done, the helper class is called an inner class.

An inner class is placed within the opening bracket and closing bracket of another class.


public class WreakMoreHavoc {

    String author = "Ignoto";



    public void infectFile() {

        VirusCode vic = new VirusCode(1024);

    }

    class VirusCode {

        int vSize;



        VirusCode(int size) {

            vSize = size;

        }

    }

}


An inner class can be used in the same manner as any other kind of helper class. The main difference—other than its location—is what happens after the compiler gets through with these classes. Inner classes do not get the name indicated by their class statement. Instead, the compiler gives them a name that includes the name of the main class.

In the preceding example, the compiler produces WreakHavoc.class and WreakHavoc$VirusCode.class.

By the Way

This section illustrates one of the simplest examples of how an inner class can be defined and used. Inner classes are an advanced feature of Java that you won't encounter often as you first learn the language. The functionality they offer can be accomplished by using helper classes defined separately from a main class, and that's the best course to take as you're starting out in programming.



     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows