This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.
This is simply syntactic sugar to switch this Map<String, List<Class<?>>> foo = new HashMap<>(); for this var foo = new HashMap<String, List<Class<?>>>();.
If you're not going to be using a proper dependency injection framework, and you want to avoid creating a factory object for every POJO you have, you still ought to be using static factory methods.
Throwing exceptions in a constructor is a Bad Thing in Java, because the partially initialized object still exists before it's garbage collected, and can allow access to instance methods which depend on the object to be fully initialized. And yes, you want to be able to throw exceptions during instantiation for fail-fast parameter validation of immutable POJOs, before invoking the private constructor.
Such method signatures can, instead of looking like public static ACustomerServiceImpl create(...), can look like public static CustomerService create(...), which would allow the programmer to write var aCustService = ACustomerServiceImpl.create(...);. Besides the compiler correctly inferring that aCustService is an instantiation of the interface rather than the implementation, this design has the added benefit of ensuring that developers cannot instantiate the implementation directly (since all of the implementation constructors are private).
73
u/Northeastpaw Mar 09 '16
This isn't about changing Java to a weakly typed language, though. Java currently determines types via RHS (with a bit of LHS scanning for the diamond operator). If you instantiate a variable with a concrete type on the RHS, why do you need to declare its type on the LHS as well? You've already stated what you want when you instantiated it.
This is simply syntactic sugar to switch this
Map<String, List<Class<?>>> foo = new HashMap<>();for thisvar foo = new HashMap<String, List<Class<?>>>();.