Issue
I have come across recommendations that the variable declaration must be an interface rather than a concrete type similar to below -
Map<String,String> myMap = HashMap<>();
List<String> myList = new ArrayList<>()
However is it acceptable in terms of coding practices to have a concrete type in variable declaration especially for local variables?
HashMap<String,String> myMap = HashMap<>();
ArrayList<String> myList = new ArrayList<>()
Solution
First the rule makes sense as it allows more powerful expressions, method parameters and method results, and is also woven into the standard java libraries.
You may change the implementation later, as method parameter the method can deal with more than one concrete implementation and hence is more versatile, less needlessly restricted. As return type it allows different dynamic implementations, List as empty list or singleton list.
Answer: No, it is not a good idea to mix styles, because of keeping your mind on a single track. There is however:
var myMap = HashMap<String, String>();
Myself, as oldie, I do not like var
but the overall style then is okay.
Answered By - Joop Eggen
Answer Checked By - Mary Flores (JavaFixing Volunteer)