DekGenius.com
Previous Section  < Day Day Up >  Next Section

Recipe 3.4 Creating a Java Class

3.4.1 Problem

You want to create a new Java class in which to enter code.

3.4.2 Solution

Select a project and then select File New Class, or right-click a project in the Package Explorer and select New Class.

3.4.3 Discussion

When you select File New Class or right-click a project in the Package Explorer and select New Class, you'll see the New Java Class dialog, as shown in Figure 3-7.

Figure 3-7. Creating a Java class
figs/ecb_0307.gif


Note the options in this dialog. You can specify the name of the package this class should be in (if the package doesn't exist, it'll be created). You can set the access modifier for the class, as well as the class's superclass, which enables you to implement inheritance. You can also specify what interfaces you want the class to implement, as well as a set of method stubs to create: main, a constructor, and inherited abstract methods.

If you want to enclose one class within another, you can enter the name of the enclosing class in the "Enclosing type" box in the New Java Class dialog. If you right-click the enclosing class in the Package Explorer and select New Class, the enclosing class's name will appear in the "Enclosing type" text box when this dialog opens; however, it won't be used unless you check the checkbox next to that text box.


3.4.3.1 Creating an anonymous inner class

In Eclipse 3.0, code assist (also called content assist) can help when you want to create an anonymous inner class. Place the cursor after the opening brace of a class instance creation and press Ctrl-Space (or select Edit Content Assist). Code assist automatically creates the body of the anonymous inner class, including all the methods you need to implement. Very cool. (For more information on code assist, see Chapter 2.)

    Previous Section  < Day Day Up >  Next Section