"Extract Method" Functionality in NetBeans?

Why does NetBeans IDE not have "extract method" functionality?

Well, as Ralph from the NetBeans Team points out, it's as simple as using Alt-Shift-M. For example, here's a big chunk of code in the constructor of the Anagram Game (which is one of the samples in NetBeans IDE):

That big chunk of code deserves its own separate method. So, as you can see, I select it all with my mouse and then I press Alt-Shift-M (or Refactor | Introduce Method), which causes the "Introduce Method" dialog to be displayed, as shown below, where I type "postInitializeGUI", as you can see below:

I click OK in the dialog above. And now the constructor contains a new reference to a method...

...which is defined elsewhere in the code:

Hurray, again, for NetBeans IDE!


I've been using Netbeans for years and never knew about this. Another excellent feature in the best Java IDE out there. Thanks for the tip!

Posted by guest on May 13, 2012 at 01:14 PM PDT #

I blogged about the cool "extract method" functionality in NetBeans IDE sometime back as well :)

Posted by guest on May 13, 2012 at 09:36 PM PDT #

It could be more useful if it made me set up the argument list.

Posted by guest on May 14, 2012 at 02:37 AM PDT #

It actually is available in the "lightbulb menu" when you highlight block of code - usually as the last position. Feature is there from as long as I remember.

It will also replace other occurences (but not very well ;] - only simplest cases) and will introduce necesary variables when used.

The only problem is that it will not try to look up for object containing all necesary information in scope - it will always produce method with most basic possible parameters (and high amount of those in consequence).

Posted by guest on May 14, 2012 at 04:55 AM PDT #

I'm writing C++ using netbeans 7.1.2. If I select a chunk of code and right-click Refactor->Introduce Method all the fields are greyed out: Introduce Variable, Introduce Constant, Introduce Field, Introduce Method.

Surely this feature isn't used only in the context of Java?!

Can someone help me figure out how to make use of this feature?

Thanks much!

Posted by guest on May 21, 2012 at 02:07 PM PDT #

Post a Comment:
  • HTML Syntax: NOT allowed

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.


« April 2014