blob: 8ba235512505900b9e4f00044e5e453f2f50f075 [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<title> Creating a Java class </title>
<link rel="stylesheet" href="../book.css" charset="ISO-8859-1" type="text/css">
</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. From the project's
context menu, select&nbsp;<strong>New &gt; Package</strong> or click the&nbsp;<strong>Create
a Java Package</strong> button in the toolbar. </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>Create a 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/qs-41.gif" width=493 height=554 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/qs-42.gif" width=297 height=476 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/qs-43.gif" width=493 height=554 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 Java preferences (<strong>Window &gt; Preferences &gt; Java &gt; Code Generation &gt; Code
and Comments</strong>).</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/qs-43a.gif" width=433 height=554 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/qs-43b.gif" width=347 height=461 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 'java.util.Vector'</em>.
This adds the missing import declaration.
<p> <img src="../images/qs-43c.gif" width=534 height=200 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/qs-43d.gif" width=430 height=199 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 Getter and Setter...</strong>.
<p> <img src="../images/qs-43e.gif" width=433 height=401 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/qs-43f.gif" width=347 height=440 alt="Generate Getter and Setter dialog">
</p>
</li>
<li> Save the file. </li>
<li> The formatting of generated code can be configured in&nbsp;<strong>Window
&gt; Preferences &gt; Java &gt; Code Formatter</strong>. If you use a prefix
or suffix for field names (e.g. fContainer), you can specify this in&nbsp;<strong>Window
&gt; Preferences &gt; Java &gt; Code Generation &gt; Names</strong> so that generated
getters and setters will suggest method names without the prefix or suffix.
</li>
</ol>
<img src="../images/ngrelc.gif" width=159 height=27 alt="Related concepts" border="0">
<p> <a href="../tasks/tasks-6.htm">Java views</a><br>
<a href="../concepts/concepts-7.htm">Java editor</a> </p>
<p> <img src="../images/ngrelt.gif" width=159 height=27 alt="Related tasks" border="0">
</p>
<p> <a href="../tasks/tasks-175.htm">Using quick fix</a><br>
<a href="../tasks/tasks-11.htm">Creating Java elements</a><br>
<a href="../tasks/tasks-43.htm">Overriding a method using the Hierarchy view</a><br>
<a href="../tasks/tasks-57.htm">Generating getters and setters</a> </p>
<p> <img src="../images/ngrelr.gif" width=159 height=27 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 generation preference page</a><br>
</p>
<p> <a href="../hglegal2003.htm"><img src="../images/ngibmcpy2003.gif" width=324 height=14 alt="Copyright (c) 2000, 2003 IBM Corporation and others. All Rights Reserved." border="0"></a>
</p>
</body>
</html>