Why NetBeans is awesome. Part 2: Intelligent Java code completion

Welcome to another part of “Why NetBeans is awesome” series. What I’d like to show you today is how does NetBeans smart code completion work. Let’s look at this simple example:

List<String> l = new 

And now let’s invoke code completion (Ctrl + Space). Here’s what you’ll see:

Notice that first things you see in this list (ones above bold line) are classes that can be assigned to List reference. This feature is something I really use a lot. I just don’t check API to get all possible implementations for interface I need to pass somewhere. Just code completion. Another thing is that generic type is also completed according to reference type.

Let’s check what it can do next with our new List. Let’s assume we need some int value from it:

and again, after Ctrl + Space what we see on top are methods returning whet we need on left side of assignment.

Now what if you have used wrong method and would like to replace it with some other method execution? Lets select old method and invoke code completion:

And what is going to be inserted?

Only the method name. NetBeans editor found that we have parenthesis opened after text we’re replacing and won’t add another one.

What about method parameters? Let’s add some variables that will fit parameters of constructor we would like to invoke and try code completion:

and here are results:

Have you noticed that there was a second list but with different generic type? Code completion has selected one that fitted better for current invocation.

In the end let’s assign object created with constructor invocation to some variable. Press Alt+Enter and select “Assign return variable to New Variable”.

You can do it also on regular methods invocations.

That’s it for today. Off course NetBeans offers a lot of other nice code aids, but these will have to wait for another part of the series.

Advertisements

One Response to Why NetBeans is awesome. Part 2: Intelligent Java code completion

  1. I’m not sure why but this website is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later on and
    see if the problem still exists.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: