Reputation: 7479
"Refer to objects by their interfaces" is a good practise, as mentioned in Effective Java. So for example i prefer
List<String> al = new ArrayList<String>();
over
ArrayList<String> al = new ArrayList<String>();
in my code. One annoying thing is that if i type ArrayList<String> al = new
and then hit Ctrl+Space in Eclipse i get ArrayList<String>()
as propostal. But if i type List al = new and then hit Ctrl+Space i will get only propostal to define anonymous inner class, but not propostals such as new ArrayList<String>()
, what is 99% the case, or for example new Vector<String>()
.
Question: Is there any way to get the subclasses as propostals for generic types?
Upvotes: 8
Views: 2457
Reputation: 21
JDK 1.7 doesn't need to specify the generic type at the right of the equal. Preference -> Java -> Compiler to 1.6
Upvotes: 2
Reputation: 22070
Depending on your personal style of writing such code, this is an alternative inspired by the answer of @Ben Schulz. If you typically write the assignment first like
al = new ArrayList<String>();
then you can use Ctrl+1
to start the "Create local variable" quick fix. Hitting Tab
will now directly open the type selection where you select the List
type.
Upvotes: 2
Reputation: 6181
I would suggest simply writing the expression first and then hitting Ctrl+2, L
. Then you can name the variable, followed by Enter, Down, Enter
. Done.
Upvotes: 9