Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

How To Guides
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Problem Solutions
Privacy Policy




Eclipse Java Development User Guide
Previous Page Home Next Page

Creating a Java class

In this section, you will create a new Java class and add methods using code generation actions.

  1. In the Package Explorer view, select the JUnit project. Click the New Java Package button in the toolbar, or select New > Package from the project's context menu .
  2. In the Name field, type test as the name for the new package. Then click Finish.
  3. In the Package Explorer view, select the new test package and click the New Java Class button in the toolbar.
  4. Make sure that JUnit appears in the Source Folder field and that test appears in the Package field. In the Name field, type MyTestCase.

    Java Class wizard

  5. Click the Browse button next to the Superclass field.
  6. In the Choose a type field in the Superclass Selection dialog, type Test to narrow the list of available superclasses.

    Superclass Selection dialog

  7. Select the TestCase class and click OK.
  8. Select the checkbox for Constructors from superclass.
  9. Click Finish to create the new class.

    Java Class wizard

  10. The new file is opened in the editor. It contains the new class, the constructor and comments.  You can select options for the creation and configuration of generated comments in the Goto the code templates preference page Java > Code Style > Code Templates preference page.
  11. In the Outline view select the new class MyTestCase. Open the context menu and select Source > Override/Implement Methods....

    Context menu of a type

  12. In the Override Methods dialog, check setUp() and tearDown() and click OK. Two methods are added to the class.

    Override Methods dialog

  13. Change the body of setUp() to container= new Vector();
  14. container and Vector are underlined with a problem highlight line as they cannot be resolved. A light bulb appears on the marker bar. Move the mouse over the underlined word Vector. A hover appears that shows the error message and contains quick fixes. Move the mouse into the hover and click Import 'Vector' (java.util). This adds the missing import declaration.

    Correction proposals hover for Vector

    The blinking cursor should still be on the line that contains the error (if not, set it to the end of the line). Press Ctrl+1. This lets the cursor jump to the nearest error location and opens the quick fix proposals. Choose Create field 'container' to add the new field.

    Correction proposals for container

  15. In the Outline view, select the class MyTestCase. Open the context menu and select Source > Generate Getters and Setters....

    Generate Getter and Setter in context menu

  16. The Generate Getter and Setter dialog suggests that you create the methods getContainer and setContainer. Select both and click OK. A getter and setter method for the field container are added.

    Generate Getter and Setter dialog

  17. Save the file.
  18. The formatting of generated code can be configured in the Goto the code style formatter perference page Java > Code Style > Formatter preference page. If you use a prefix or suffix for field names (e.g. fContainer), you can specify this in the Goto the code style preference page Java > Code Style preference page so that generated getters and setters will suggest method names without the prefix or suffix.
Related concepts

Java views
Java editor

Related reference

New Java Class wizard
Source actions
Quick fix
Override methods dialog
Generate Getter and Setter dialog
Code formatter preference page
Code style preference page
Code templates preference page

  Published under the terms of the Eclipse Public License Version 1.0 ("EPL") Design by Interspire