Eclipse IDE provides multiple functionalities which can help you when you are coding. The functionalities what I am talking about are typically very small and easy to use actions such as Content Assistant, Quick Fix, Refactoring and other code manipulations. Usage of those features can improve speed and quality of your coding.
In this article and also in the other articles under label coding tips I would like to describe usage of those features in the way where you will write as little code as possible and the rest of code will be generated by Eclipse.
Let start with very simple example - assignment of new instance of StringBuffer class to the local variable or to the field.
So what we want to have at the and will be something like:
StringBuffer buffer = new StringBuffer();
- write keyword 'new'
- write 'S' and push Ctrl+Space (shortcut for Content Assistant)
you can see list of classes, interfaces and templates starting with 'S'
- you can specify more accurately the name of requested class writing its following letters 'tr' or using Camel feature - just write 'B' as a Buffer. Then navigate selection to StringBuffer class.
- press Enter. The name of the class is generated.
- write '(' as an open parenthesis (the closing parenthesis is generated automatically) for constructor parameters and and push Ctrl+Space again.
- select constructor you want to use and press Enter
- write down the parameter values for the constructor, press End button, add semicolon ';' and save (off course using Ctr+S shortcut).
- press Ctrl+1 (cursor should be somewhere on the line code). It is shortcut for Quick Fix proposal.
- choose one of the proposals and press Enter. The Eclipse then generates all necessary code to have it compile able.
For more advanced usage you can use direct shortcuts for proposed actions (you can see them in the proposal) - Ctrl+2, L for assignment to local variable and Ctrl+2, F for assignment to field.
It also provides you possibility to give a name of created variable or field.
- When you are sure about the name of the variable press Enter. It will offer you to specify type of variable in more detail. Of course the proposed types are super types of the type you instantiated.
- When you will choose the right type press siply Enter again and once more for finalization.