Step 7.3: Creating a new class


Step 7.3: Creating a new class

click to expand

Creating a class is quite easy, but how you do it depends on what you plan to do with the class. For this step, you'll be creating a class that you can actually run from the command line (or the console, as it's called within Eclipse).

q 7.3(a) Right-click on Hello in the Package Explorer and select New Class.

click to expand
Figure 7.8: Creating a new class using New/Class.

q 7.3(b) Make sure the Source Folder is Hello.

q 7.3(c) Leave Package blank.

q 7.3(d) Leave Enclosing type unchecked.

q 7.3(e) Enter HeIloWorld in the Name field.

q 7.3(f) Make sure public is checked, abstract and final are unchecked.

q 7.3(g) Leave Superclass as java.lang.Object, Interfaces blank.

q 7.3(h) Make sure public static void main(String[] args) is checked.

q 7.3(i) Leave the other two unchecked.

q 7.3(j) Click Finish.

click to expand
Figure 7.9: Setting the fields properly for a new class.

Following these steps will create a class HeIloWorld in the default package (because you left Package blank) in project Hello. It will be a public class, neither abstract nor final. The class has no superclass (except for the implied superclass Object) and implements no interfaces. Finally, the IDE has been instructed to create a standard "main" method, but no other methods . That being the case, we will see the result as in Figure 7.10.

click to expand
Figure 7.10: The result of the addition of the new class HelloWorld.

There are a number of interesting results from this simple operation.

  1. A default package is added, with the icon. The default package is added because you left the package name blank in Step 7.3(c). Had you entered a package name, that package would have been created instead. Had you used an already existing package, the class would simply have been added to that package.

  2. The class HelloWorld was added. More correctly, the Java source code was added. That's what the icon in the Package Explorer indicates. The class is also created; we'll look into this a little further in a moment.

  3. A class is added with some initial comments. These comments are pretty useless at this point, but you can change them.

    Note  

    To change the default documentation for a new class, go into Window/Preferences. In the Preferences dialog, select Java/Templates and then edit the template named typecomment. You can modify quite a number of templates using this dialog.

  4. The class itself is defined as HelloWorld. You'll also notice that the class is shown in the Outline view.

  5. Last but not least, the main() method is created. It, too, has an entry in the Outline view.




Eclipse
Eclipse: Step by Step (Step-by-Step series)
ISBN: 1583470441
EAN: 2147483647
Year: 2003
Pages: 90
Authors: Joe Pluta

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net