Creating a Scrapbook Page in Eclipse

Eclipse provides several tools to simplify the construction of Java programs. It is among the most sophisticated IDEs out there, with thousands of new features added by programmers every day.

Some of the features in Eclipse are innovative as well. One of these is the possibility of executing code on the fly, much like it is done on dynamic languages such as Python, Ruby, or Lisp. This feature is called "Java Scrapbooks", and it is little know by most developers.

The Scrapbook Way

A Java scrapbook in Eclipse is just a file where you can enter and evaluate Java expressions, much like you would do in the REPL of languages such a Python, Ruby, or Lisp. In the scrapbook file you can evaluate the Java expressions using context menu options such as "inspect", "execute", or "display".

To create a new scrapbook page, you can use the following menu option: File-new-other. Then, select Java and finally a new Java scrapbook. The create file dialog will request the name of the file where you want to save the resulting scrapbook.

Remember that a scrapbook is not to meant to save real Java code (such as complete classes). The objective is just to provide a simple way to interact with the objects in the language -- in a similar way that would be possible in a dynamic language such as Smalltalk.

Interacting with the Scrapbook

To interact with a scrapbook, you can just type a Java expression. For example:

System.out.println("hello world")

To execute this, you can select the expression, then use the context menu and click the "execute" option. This will run this snippet of code, and the result will be displayed in the console window.

There are two other ways to interact with the scrapbook using the context menu:

Conclusion

Java Scrapbooks in Eclipse are a very useful feature for testing Java code. It provides some of the tools previously available only on dynamic languages, and it allows quick validation of code.

Scrapbooks are also little known by Eclipse users, despite the many potential uses. Getting acquainted with this feature may turn you into a more productive programmers, and positively affect the quality of your code.

Another advantage of using scrapbooks is that this kind of evaluation may lead to code that is easier to test in general. In other words, using a scrapbook promotes writing code that is decoupled from other code, and this is good for testability.

Further reading

Here is another page describing how to use scrapbooks in Eclipse.

Tags: languages, dynamic, development, programming, Eclipse
Article created on 2009-04-06 10:34:05

Post a comment