Experimenting with Code Fragments

There may be times you have a fragment of Java code that you want to try out before adding it to a class or project in the workspace.  Along with being a good place to keep notes and lists of ideas, the Scrapbook also provides a flexible environment for testing and experimenting with any piece of Java code.  Contents of Scrapbook pages are saved to files, not to the repository.

To open the Scrapbook, select Scrapbook from the Window menu of any IDE window. 

Running Code Fragments in the Scrapbook
To run a code fragment in the Scrapbook:

  1. On a page in the Scrapbook, type in Java expressions and statements, or copy and paste them from another source.  Alternatively, open a file that contains Java code.
  2. Select all or part of the code on the page by highlighting it with the cursor.   Only the highlighted code will be run.
  3. Select the Run button runup.gif (1037 bytes) from the toolbar.

While the page is running the code fragment, its page symbol changes to indicate that it is busy.  No other code can be run from that page while it is busy.

Resetting a Busy Page
To stop a running code fragment:

  1. Click the tab for a Scrapbook page that is running a code fragment.
  2. Select Restart Page from the Page menu.

When a page is reset, all threads started from the page are terminated, and all classes in the code are uninitialized.  The compilation context and the text on the page do not change.

Changing the Compilation Context
When the code fragment runs, the IDE assumes it belongs to a particular class and package that exists within the workspace.  This class provides a compilation context for the fragment, which determines what other program elements are inherited by and accessible to the fragment.  Each Scrapbook page has one compilation context, which is by default java.lang.Object

To select another compilation context:

  1. Click the tab for the Scrapbook page for which you want to change the context.
  2. Select Run In from the Page menu in the Scrapbook.
  3. Select a package and a class or interface from the lists.
  4. Click OK.

The information line at the bottom of the Scrapbook page indicates the compilation context for the page.  All code fragments run from the page will use this compilation context.

Inspecting and Debugging Code Fragments
Just as you can inspect variables for code in the workspace, you can also inspect variable values in code fragments in the Scrapbook.  To inspect a code fragment in the scrapbook, select the code that you want to inspect, and then click the Inspect button inspectUp.gif (1056 bytes).

If the selected code returns a result that can be inspected, an Inspector window is launched.  If the selected code does not return a result that can be inspected, the following message  is displayed on the page:

No explicit return value.

Similarly, you can debug the code fragment by using the integrated debugger.  To launch the debugger, select the code you want to debug, and then click the Debug button bug2up.gif (1107 bytes).

Example:  Running Code in the Scrapbook

ngrelc.gif (533 bytes)
The Scrapbook
Workspace

ngrelt.gif (466 bytes)
Organizing Your Ideas in the Scrapbook
Debugging During the Development Cycle with the Integrated Debugger

ngrelr.gif (548 bytes)
VisualAge for Java IDE Symbols