Why NetBeans is awesome. Part 2: Intelligent Java code completion
May 22, 2011 1 Comment
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.