Place
the cursor over this icon to load and view all
the screenshots for this tutorial. (Caution: This action loads all screenshots
simultaneously, so response time may be slow depending on your Internet connection.)
Note: Alternatively, you can place the cursor
over an individual icon in the following steps to load and view only the screenshot
associated with that step. You can hide an individual screenshot by clicking it.
Overview
In the tutorial, you learn how to create a simple Java class.
Using your new class, you then explore some of the features of the JDeveloper
IDE, including the Code Assist and the Debugger..
Have access to or have installed Oracle JDeveloper (10.1.3.1.0). You can download it from Oracle
Technology Network. Unzip to a directory of your choice, which in this tutorial is referred to as JDEV_HOME.
Start JDeveloper. Double-click the JDeveloper executable (jdeveloper.exe) found in the JDEV_HOME directory.
If a dialog box opens that asks if you would like to migrate from a previous version of JDeveloper, click NO.
Close the Tip of the Day window.
2.
Once loaded, the JDeveloper IDE appears. The very first time you open JDeveloper, the Start
Page displays. You can re-invoke the Start Page later by choosing
Help | Start Page.
Notice the various options available to help you learn about JDeveloper. After exploring these options, close the Start Page by clicking the X on its tab (the X appears when you mouse over the tab).
The application is the highest level in the control structure. It is a view of all the objects you need while you are working. An application keeps track of your projects while you are developing your Java programs.
Applications are stored in files with the extension .jws. When you open JDeveloper, the last application used is opened by default, so you can resume where you left off.
When creating a new application in JDeveloper, you have the option to base it on a template. The application template that you select determines the initial project structure, that is, the named project folders within the application. You can alter existing templates or create new ones.
In JDeveloper you always work with projects contained in an application. A JDeveloper project is a logical grouping of files that are related. A project keeps track of the source files, packages, classes, images, and other elements that your program may need. You can add multiple projects to your application to easily access, modify, and reuse your source code.
Projects manage environment variables such as the source and output paths used for compiling and running your programs. Projects also maintain compiler, run-time, and debugging options, so you can customize the behavior of those tools per project.
The Applications Navigator is the main JDeveloper window from which you access
the components of your application. The structure of the Applications Navigator is hierarchical and supports
applications, projects, images, .html files, and more. By default, JDeveloper displays the Applications Navigator on
the left side of the IDE .
To create an application, perform the following steps:
1.
Right-click the Applications node in the Applications Navigator and select New Application... from the context menu.
2.
In the Create Application dialog, modify the default application name Application1 to MyFirstApp.
Note that the Directory Name changes accordingly.
3.
Click the downward-pointing arrow in the Application Template field to invoke the dropdown list of available templates. Click the No Template[All Technologies] list entry to configure this application with a single project that has access to all JDeveloper technologies.
Click OK.
4.
In the Create Project dialog, change the default Project Name Project1 to MyProject, and then click OK.
5.
In the Applications Navigator, projects are displayed as the second level in the hierarchy under the application. The Applications Navigator should look like this:
Note that the project folder is a child of the application folder.
To create a new Java class, perform the following steps:
1.
Right-click the MyProject node in the Applications Navigator
and select the New... option from the context menu.
2.
The New Gallery displays. By default, the first category, General,
is highlighted in the Categories list. The other
categories enable you to work with different technologies to build
the various tiers of an application.
Click the + sign to the left of
the General category to expand it, and take note of the sub-categories
that are available.
Select the Simple Files node, and then select Java Class in the Items list in the right portion of the window. Click OK.
3.
In the Create Java Class dialog, change the default name Class1 to Dog.
Because you did not specify a package name when creating the application, the package name defaults to the project name in lowercase. You could change this if desired, but you do not need to do so for this tutorial.
Accept all other defaults and click OK.
4.
The new class opens automatically in the source editor, where you see
the skeleton class definition.
Add a method to the class. After the constructor, press [Enter] to create a new line, and type the following code:
public String sayHi()
Notice that the line that you just entered has a wavy red line under it, indicating a problem with the syntax. There are also red boxes in the right margin of the source editor. If you mouse over these boxes or over the wavy red line, a tooltip displays information about the error or errors.
You see other examples like this in Working with Code Assist later in this tutorial.
5.
At the end of the line, press [Ctrl]+[Shift]+[Enter]. JDeveloper then provides auto-completion of the method structure, and the error indicators disappear. A green box appears in the upper right margin to show that there are no syntax errors.
6.
In the blank line between the two curly braces that were added to the method, add the following code to return a string:
return " woof " + "Kate";
Notice that when you type the first double quote symbol ("), JDeveloper automatically provides you with the second double quote, enclosing the cursor between them, so that you can easily type the literal.
Editors are where most of the application
development work takes place; this is where you write code
and design user interfaces. In this topic you explore some of the features of
the Java Source Editor.
Code templates assist you in writing code more quickly
and efficiently while you are in the source editor. You can edit existing templates
or create your own. This topic shows you how to use some of the existing code
templates in the Dog class.
1.
There is a code template for adding a main method
to a Java class.
Press[ Enter] to create a new line after the sayHi() method.
Type the letter m, and press [Ctrl]+[Enter] to
invoke code templates.
The main method template
is suggested.
2.
Press Enter again to accept the suggestion and incorporate
the template.
3.
Create a Dog object in the main method by adding the following code:
Dog
myDog = new Dog();
Note: The myDog variable
is displayed in gray because it has not yet been used anywhere in the
class. If you mouse over the myDog variable, a tooltip displays to tell you that the variable has not been used. You see other examples like this in Working with Code Assist later in this tutorial.
4.
Press [Ctrl]+[Enter] to see the list of code templates
that are available.
5.
You decide to create an integer-based loop using the fori code
template.
In a new line after the line that creates the mydog variable, typef and then press [Ctrl]+[Enter].
Three templates are suggested.
6.
Double-click the second of the three suggestions, the fori integer-based
loop, to select it.
The template code is incorporated into the file.
7.
Modify the template code.
Replace i with count and limit
the loop to 3 iterations.
Notice that changing the first i variable name in the
loop changes all subsequent references.
8.
Enter a System.out.println statement.
Place the cursor
on the blank line inside the curly braces of the for
loop, and type System. (be sure to include the dot at the end of the word.) A list of suggested code appears.
Type the letter o and press [Enter] to select the suggested code.
Type . (dot), and when the list of suggested code appears, type the letter p and double-click the println() suggested code.
Note: A quicker way to enter a System.out.println() statement is to enter sop and then press [Ctrl]+[Enter]. The technique above is given to illustrate how to use code completion.
9.
Add code to use the loop to display the sayHi message. Inside the
parentheses after println, enter the following code:
count
+ myDog.sayHi()
The complete line should read:
System.out.println(count + myDog.sayHi());
10.
Right-click within the editor view and select Reformat to have JDeveloper restructure your code.
When you successfully compile a .java file, you create a .class file in the \src directory of the project. Compiling a class in JDeveloper automatically saves the .java file as well. When you run a class, it is automatically compiled and saved.
1.
In the Applications Navigator or in the source editor, right-click Dog.java
and select Make from the context menu.
2.
At the bottom right of the JDeveloper IDE, the log window should show successful compilation. If the log window does not display, use View | Log
to display it ( or press [Ctrl]+[Shift]+[L]).
Notice that when using the Make option to compile your class, JDeveloper
saves all the files in your project.
3.
In the Applications Navigator or in the source editor, right-click Dog.java
again, and this time select Run from the context
menu.
.
4.
The log window displays 3 counts of the ' woof Kate' message.
Code Assist examines your code in the editor and provides
assistance to fix common problems. Here you use the Dog class to explore some
examples of the suggestions that are offered.
1.
Create a Cat object.
At the start of the main
method, just after the first curly brace, press [Enter]
to create a new line. In the new line, enter the following code:
Cat
myCat = new Cat();
2.
Notice that a light bulb icon has appeared to the left of the new
line. Place your mouse over this icon and the message "Quick
fixes and code assists for this line" displays.
3.
Click the light bulb icon to see the suggested fixes.
Notice the different colors of light bulbs beside the suggestions,
and different signs inside them. The amber light bulb containing a question
mark (?) indicates a suggestion to improve your code, while the red light bulb containing an exclamation
point (!) suggests a way to correct an error.
4.
You need a Cat class in order to instantiate a Cat object. If you click the
first suggestion, JDeveloper invokes the Create Class dialog where you
could create a Cat class.
5.
You do not create a Cat class just now, so click Cancel
to close the Create Class dialog. However, remind yourself to do it
later by doing the following:
Press [Enter] after new Cat(); to open a new
line. In the new line enter the following code:
//TODO create a Cat class
6.
Select View | Tasks Window to see a list of tasks
that you have created.
7.
The Tasks window displays a list of the tasks you have created (in
this case, it is your only task).
8.
If you double-click a task in the list, JDeveloper takes you to the relevant
task, inserting the cursor at the start of the line.
9.
Notice the red box at the top right margin of the editor. Moving the mouse over this icon informs
you that there are errors in your program.
10.
A smaller red box at the appropriate point in the code gives
more information about the error.
11.
The pink markers indicate where you have created a task. Hover over
the marker with the mouse to see what the task is.
12.
Comment out the line that creates the Cat object. Notice that the red markers have now
disappeared, to be replaced by a green marker indicating that there
are no errors in your code.
13.
Right-click within the editor window and select the Reformat option.
Your code should look like this:
Refactoring is an editing technique that modifies code
structure without altering program behavior. A refactoring operation
is a sequence of simple edits that transforms a program's code but keeps it
in a state where it compiles and runs correctly.
JDeveloper provides a collection of refactoring operations.
1.
One example of a refactoring operation is replacing a constant
expression in a method
body
by
a parameter
for
the method.
The
expression is replaced by the parameter name. The new parameter is added
to the method's parameter list and to all invocations of the method.
To do this in the Dog class, right-click the literal, 'Kate' in the sayHi() method code
and select Refactor | Introduce Parameter...from the context menu
2.
In the Introduce Parameter dialog, type p_name
in the Name field, and click OK.
3.
Examine the code to see the results of the refactor operation. The method
declaration now contains String p_name
in its parameter list; the parameter p_name
has replaced the literal 'Kate' in the method return value, and the literal
'Kate' has been inserted as a parameter in the method call.
4.
Another refactoring operation is to derive a new interface from selected
methods in an existing class.
To do this in the Dog class, right-click the Dog class declaration method, and from the context menu, choose Refactor
| Extract Interface...
5.
In the Extract Interface dialog, type IntAnimal
as the name of the interface, and select the sayHi(String)
method in the Members to Extract list. Click OK.
6.
The IntAnimal interface is created and opens
in the source editor.
7.
Another simple refactoring operation is to rename a method, whereby every occurrence
of the method name is replaced by the new name.
To do this in the IntAnimal interface, right-click in
the sayHi() method,
and from the context menu, choose Refactor | Rename.
8.
In the Rename Method dialog, change the sayHi
method name to sayHowDoYouDo. Select the Preview
check box to see all the usages that are affected by the name change. Click
OK.
9.
The log window lists all usages of the sayHi()
method. You should examine each usage to check that you want each occurrence
of sayHi()to be changed
to sayHowDoYouDo(). If
so, click Do Refactoring in the log window
toolbar.
10.
Note that the name change has taken place in the IntAnimal
interface......
JDeveloper has a built-in history feature. This local history
does not require a version control system to provide a recent
change record and visual "diff" between versions. Versions are automatically
created based on user interactions such as
Save, Compile, Rename, and so on.
1.
Notice the three tabs at the foot of the editor window.
Click the History tab.
2.
The History window displays. The top part of the window contains a list of revisions and dates, while a list of changes to the code displays in the bottom part of the window. The two windows
are synchronized, so that the detail in the bottom part of the window
matches the selection in the top part.
The revisions for the selected
date and time are summarized in the status line at the bottom of the IDE, in this case 3
differences: 0 added, 0 removed, 3 changed.
3.
The lilac-colored boxes indicate changes to the
code.
In the top portion of the window, select Introduce Parameter at the top of the list.
In the bottom left portion of the editor, position your mouse over the green right-pointing arrow in the lilac
box that contains the sayHi() method declaration. Notice that a message displays, indicating that
clicking the green arrow enables you to replace the adjacent difference. In this case, clicking the green arrow would revert the sayHowDoYouDo() method to sayHi().
4.
The green boxes indicate additions to the code.
In the top portion of the window, select Expand Statement near the bottom of the list.
In the bottom of the window, select the return "woof " + p_name; line in the green
box in the right hand window. Hover over the red X with your mouse.
Notice the message indicating that to delete the addition(s), you click
the X.
JDeveloper provides easy ways to navigate to related code and to Javadoc, and the ability to expand or contract sections of code improves navigability. To explore these features, perform the following steps:
1.
Click the Source tab for the Dog.java file in the editor.
You can navigate from one part of the code to another related
part. One example of this is navigating from a method to its declaration
in an interface.
A method that implements a declaration from an interface displays
a call out icon in the left-hand margin. Clicking this icon takes
you to where the method is declared.
Click the
i icon next to the sayHowDoYouDo() method in the Dog.java file.
2.
JDeveloper takes you to the IntAnimal interface where the method is declared
and highlights the method name for you.
3.
To return to where you were in the Dog class, click the green
Back button in the toolbar.
4.
You can also navigate to the Javadoc for a given element. In the Dog.java file
in the editor, right-click in the parameter list
for the sayHowDoYouDo()
method. From the context menu, choose Quick Javadoc.
5.
The Javadoc tag for the String object is displayed.
6.
Code folding enables you to expand and contract sections of code,
making large programs more navigable.
Place your mouse in the space between the dotted and solid lines
to the left of the Cat line.
Notice that a blue vertical line displays beside the main method
body.
7.
Click the minus (-)
sign at the top of the vertical blue line to contract this section
of code.
8.
Hover over the plus (+)
sign next to the contracted section of code. The contracted lines
of code display in a blue shaded box.
The integrated JDeveloper debugger enables you to debug
Java programs in the source editor. This topic shows
you how to control the execution of a program by setting breakpoints. When
program execution encounters a
breakpoint, the program pauses, and the debugger displays the line containing
the breakpoint in the source editor. You
can then use the debugger to view the state of the program.
1.
Set a breakpoint in the Dog.java
file.
To do this, click in the margin to the left of the line: System.out.println(count
+ myDog.sayHowDoYouDo("Kate"));
The breakpoint icon, which looks like a red ball, is displayed in the margin.
2.
Right-click in the source editor and select Debug
from the context menu.
3.
Program execution proceeds up to the breakpoint. The red arrow
in the left margin of the source editor indicates where the break
is occurring. The debugger window opens and displays the debugging trace.
4.
Click the Step Over icon in the toolbar of the debugger log window to execute the first iteration of the myDog.sayHowDoYouDo()
method.
5.
Note that the log window displays the first woof Kate message.
6
Notice the Smart Data window at the bottom right
of the screen. Select the count variable, and double-click
in the Value column to display the Modify
Value dialog.
7.
Type 2 as the new value. Click OK.
8.
In the debugger log window toolbar, click Resume to
continue program execution.
9.
The count variable is incremented and exceeds its limit,
so the program terminates, and the debugger disconnects.
This tutorial gave you a basic programming tour of the
JDeveloper IDE. You created an application, a project, and a Java class. You then used the
class to explore a number of features of the Java IDE, including
incorporating code templates, using Code Assist, refactoring,and reviewing your code modification
history. Finally, you saw how to debug your program by using the integrated debugger.
You've learned how to:
Create
a new application and project in JDeveloper
Create a new Java
class
Exploit some of the
productivity and assistance features of the Java IDE