Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse shortcut to declare a variable name as method parameter

Tags:

java

eclipse

Let's say we had a variable like this:

byte[] someByteArray;

And a method prototype like this:

public void someMethodRequiringString(String someByteArray);

And then we wanted to call our method on our byte array:

someMethodRequiringString(someByteArray);

Now we realize that our method requires a String instead. Let's also say we had already a conversion function in package Util called convertFromByteArrayToString(). What's the best way to get from

someMethodRequiringString(someByteArray);     

to

someMethodRequiringString(Util.convertFromByteArrayToString(someByteArray));

My way is to put the cursor in front of someByteArray and type in Util., hit CTRL+Space and Eclipse suggests the method name of the conversion function. But, once I hit enter to choose this function, I will end up with something like this:

 someMethodRequiringString(Util.convertFromByteArrayToString(bytearray)someByteArray);

where bytes is the input parameter name declared in Util.convertFromByteArrayToString(byte[] bytearray);. I know this might be a beginner's question but what's the best practice here?

like image 572
Martin Lundberg Avatar asked Oct 22 '22 16:10

Martin Lundberg


1 Answers

You may want to have code completion overwrite the existing code (instead of inserting it before the existing text) as described here: http://www.vogella.com/articles/Eclipse/article.html#tips_completion

Alternatively you may also consider deleting the already written method argument someByteArray hitting CtrlDelete 3 times (if your cursor is in front of it) or CtrlBackspace 3 times (if your cursor is directly after the argument). Then you add your Utils method via code completion as you do now and then you have the byte array argument be re-added by code completion (as Eclipse will suggest your variable as best choice, even without any character written yet).

The second alternative may sound complicated at first, but should make you type this much more quick if used often.

like image 90
Bananeweizen Avatar answered Oct 24 '22 11:10

Bananeweizen