blob: cbe537e59e0d1fa6e1be2c7ff4e715d9476e010a [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta name="copyright" content="Copyright (c) IBM Corporation and others 2000, 2007. This page is made available under license. For full details see the LEGAL in the documentation book that contains this page." >
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<title> Creating a Java class </title>
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
<script language="JavaScript" src="PLUGINS_ROOT/org.eclipse.help/livehelp.js"> </script>
</head>
<body>
<h1> Creating a Java class </h1>
<p> In this section, you will create a new Java class and add methods using
code generation actions. </p>
<ol>
<li> In the Package Explorer view, select the JUnit project.
Click the&nbsp;<strong>New Java Package</strong> button in the toolbar, or select&nbsp;<strong>New &gt; Package</strong> from the project's
context menu . </li>
<li> In the&nbsp;<strong>Name</strong> field, type&nbsp;<em>test</em> as the
name for the new package. Then click&nbsp;<strong>Finish</strong>. </li>
<li> In the Package Explorer view, select the new&nbsp;<em>test</em> package
and click the&nbsp;<strong>New Java Class</strong> button in the toolbar.
</li>
<li> Make sure that&nbsp;<em>JUnit</em> appears in the&nbsp;<strong>Source Folder</strong>
field and that&nbsp;<em>test</em> appears in the&nbsp;<strong>Package</strong>
field. In the&nbsp;<strong>Name</strong> field, type&nbsp;<em>MyTestCase</em>.
<p> <img src="images/wiz_new_class_dialog.png" alt="Java Class wizard">
</p>
</li>
<li> Click the&nbsp;<strong>Browse</strong> button next to the&nbsp;<strong>Superclass</strong>
field. </li>
<li> In the&nbsp;<strong>Choose a type</strong> field in the Superclass Selection
dialog, type&nbsp;<em>Test</em> to narrow the list of available superclasses.
<p> <img src="images/superclass_selection_dialog.png" alt="Superclass Selection dialog">
</p>
</li>
<li> Select the&nbsp;<em>TestCase</em> class and click&nbsp;<strong>OK</strong>.
</li>
<li> Select the checkbox for&nbsp;<strong>Constructors from superclass</strong>.
</li>
<li> Click&nbsp;<strong>Finish</strong> to create the new class.
<p> <img src="images/wiz_new_class_dialog_superchecked.png" alt="Java Class wizard">
</p>
</li>
<li> The new file is opened in the editor. It contains the new class,
the constructor and comments.&nbsp; You can select options for the creation and
configuration of generated comments in the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.CodeTemplatePreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Goto the code templates preference page">
<strong>Java &gt; Code Style &gt; Code Templates</strong></a>
preference page.</li>
<li> In the Outline view select the new class&nbsp;<em>MyTestCase</em>. Open
the context menu and select&nbsp;<strong>Source &gt; Override/Implement Methods...</strong>.
<p> <img src="images/editor_mytestcase_outline_override.png" alt="Context menu of a type">
</p>
</li>
<li> In the Override Methods dialog, check <em>setUp()</em> and <em>tearDown()</em>
and click&nbsp;<strong>OK</strong>. Two methods are added to the class.
<p> <img src="images/override_implement_dialog.png" alt="Override Methods dialog">
</p>
</li>
<li> Change the body of <em>setUp()</em> to <code>container= new Vector();</code> </li>
<li> <em>container</em> and <em>Vector</em> are underlined with a problem
highlight line as they cannot be resolved. A light bulb appears on the marker
bar. Set the cursor inside <em>Vector</em> and press <kbd>Ctrl+1</kbd> (or use
<strong>Edit &gt; Quick Fix</strong> from the menu bar). Choose <em>Import 'Vector' (java.util)</em>.This adds the missing import declaration.
<p> <img src="images/editor_mytestcase_import.png" alt="Correction proposals for Vector"><br>
</p>
Set the cursor inside <em>container</em> and press <kbd>Ctrl+1</kbd>. Choose <em>Create
field 'container'</em> to add the new field.
<p> <img src="images/editor_mytestcase_field.png" alt="Correction proposals for container">
</p>
</li>
<li> In the Outline view, select the class <em>MyTestCase</em>. Open the
context menu and select <strong>Source &gt; Generate Getters and Setters...</strong>.
<p> <img src="images/editor_mytestcase_getters.png" alt="Generate Getter and Setter in context menu">
</p>
</li>
<li> The Generate Getter and Setter dialog suggests that you create the methods getContainer
and setContainer. Select both and click <strong>OK</strong>. A getter
and setter method for the field container are added.
<p> <img src="images/generate_getter_setter_dialog.png" alt="Generate Getter and Setter dialog">
</p>
</li>
<li> Save the file. </li>
<li> The formatting of generated code can be configured in the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.CodeFormatterPreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Goto the code style formatter perference page">
<strong>Java &gt; Code Style &gt; Formatter</strong></a> preference page.
If you use a prefix or suffix for field names (e.g. fContainer), you can specify this in the
<a class="command-link" href='javascript:executeCommand("org.eclipse.ui.window.preferences(preferencePageId=org.eclipse.jdt.ui.preferences.CodeStylePreferencePage)")'>
<img src="PLUGINS_ROOT/org.eclipse.help/command_link.png" alt="Goto the code style preference page">
<strong>Java &gt; Code Style</strong></a> preference page so that generated
getters and setters will suggest method names without the prefix or suffix.
</li>
</ol>
<P class="nav_footer" id="nav_footer">Next Section: <A href="qs-8.htm">Renaming Java elements</A>
</P>
<img src="../images/ngrelc.png" alt="Related concepts" border="0">
<p> <a href="../concepts/cjavaviews.htm">Java views</a><br>
<a href="../concepts/concepts-7.htm">Java editor</a> </p>
<p> <img src="../images/ngrelr.png" alt="Related reference" border="0">
</p>
<p> <a href="../reference/ref-126.htm">New Java Class wizard</a><br>
<a href="../reference/ref-menu-source.htm">Source actions</a><br>
<a href="../reference/ref-java-editor-quickfix.htm">Quick Fix</a><br>
<a href="../reference/ref-dialog-override-method.htm">Override Methods dialog</a><br>
<a href="../reference/ref-dialog-gettersetter.htm">Generate Getter and Setter dialog</a><br>
<a href="../reference/ref-17.htm">Code formatter preference page</a><br>
<a href="../reference/ref-preferences-code-generation.htm">Code style preference page</a><br>
<a href="../reference/ref-preferences-code-templates.htm">Code templates preference page</a><br>
</p>
</body>
</html>