So far we’ve created a NetBeans project and written and compiled our first class. What do we have to show for it? A few directories and a Java file.

Big deal.

Thankfully, that’s just the beginning. Now that we’re at a point where we can start to code, the fun stuff begins. Ultimately I want to cover writting/running test cases, using a debugger, refactoring, and building/running executable JARs. But first, let’s take a look at some of the perks the NetBeans editor gives us.

Autocomplete

Autocomplete is one of my most used features in an IDE. The idea is that since Java is strongly typed* you can have NetBeans tell you possible methods that can be executed on an object given its type. To trigger the autocomplete, type the period after the object reference and press ctrl+space. A drop-down will appear showing the possible methods that can be invoked on that object.

* If you don’t know what that means just yet, your professor had better cover it. :)

NetBeans - Autocomplete

In the above example, the drop-down shows all methods that can be executed on the out object. Most students will be familiar with the println method, however the autocomplete shows there are a number of other methods exposed by the out object that can be called. Keep in mind that in this case I didn’t scroll down far enough to show the println(String s) method which most students have used in the past. It’s still there, it’s just not shown in the screenshot.

Below the drop-down is the javadoc for the currently selected method (println in this case). This displays whatever documentation the author felt was necessary to properly use the method, including a description of what is returned and explanations of any parameters the method takes.

Bitter Old Coder Comment: This is a mixed bag. On one hand, it helps students more readily see what can be done with an object, (hopefully) inspiring experimentation. However, don’t become so reliant on autocomplete that the ability to read an API gets sacrificed in the process. You never know when you’re going to have to use a plain text editor which won’t have these features (but trust me, at some point, you will).
Tip: You can begin to type the name of a method before pressing ctrl+space. The drop-down will only display entries that could finish the text you’ve already typed. So in our System.out example, if we type “prin” before pressing ctrl+space, NetBeans will only show methods that begin with “prin”.

Syntax Highlighting

Take another look at the above screenshot. Notice how the System.out snippet is underlined with a wavy red line (is there an actual name for that style of line?). Also notice the line number has been replaced with a warning indicator. Both warnings indicate that the line is invalid and won’t compile. In this case, what we’ve typed (System.out) isn’t a valid line of code. Once this is completed, for instance with a call to println and a semicolon, the warning indicators will go away.

This is a great teaching assistant for new programmers. Compiler errors can be… well, they aren’t always all that clear. Hovering over the code marked as invalid will display a tool tip describing the error. In this case, hovering over the snippet displays “not a statement”. This can be a tremendous help when still learning the syntax of the language. It’s not just for beginners either. Many veteran programmers appreciate the extra pair of virtual eyes that the IDE provides through this mechanism.

Code Templates

I referred to this in an earlier blog entry, mistakenly using the IntelliJ term “Live Templates”. The concept is the same in both IDEs. The user types a short abbreviation and uses the tab key to have the IDE automatically expand it into full code.

To see the templates provided by default in NetBeans, navigate to the Tools -> Options menu item, select Editor from the top set of icons, and Code Templates from the tabs below. There are a number of them defined, so rather than sit there attempting to memorize them all, here’s a quick list of ones I regularly use and the text they expand into:

  • psvm – public static void main
  • psfs – private static final String (useful for defining local constants)
  • sout – System.out.println()

You’d be surprised how useful it becomes, especially if you’re a slow typer. Between these and the autocomplete, we’ve already addressed two powerful features to limit how much code you actually have to manually type. I’d guess I only ever fully type out about 10% of the code I write. :)

In addition to the default templates, users can create their own. This can come in especially handy when you’re working with a particularly verbose API. I won’t get into the full specifics on the syntax of defining a template now, but it’s worth taking a quick look at the “sop” template:

System.out.println("${cursor}");

Notice the odd ${cursor} piece in the middle. That’s an example of the variable capabilities in the template definition. The ${cursor} snippet will leave the cursor in that position after the text is expanded. That makes sense, since you’ll likely want to type something into the parameter (notice the quotation marks are included in the template definition).

There are more possibilities for these syntax variables. The help button on that screen provides a link with more description. I’m not necessarily suggesting that a new Java developer dive right into defining complex code templates, but at least knowing it is there for future reference is valuable.

Comment a Line of Code

Pressing ctrl+/ at any point in a line of code will comment out the entire line of code. Pressing it again will uncomment the line. It may sound dumb, but when you’re incrementally debugging a block of code, you may find yourself using this quite frequently.

I’ll stop there for now. Too much at once will just start to melt brains. :)

I don’t have a specific “Editing Part 2″ in mind just yet, but I’m sure there are editing features I cannot live without that I have yet to touch on. We’ll see where my blogging motivation takes me next in this series.

Comments are closed.