Understanding the concept behind Service provider framework like JDBC using the factory method

Consider something like the following: public interface MyService { void doSomething(); } public class MyServiceFactory { public static MyService getService() { try { (MyService) Class.forName(System.getProperty(“MyServiceImplemetation”)).newInstance(); } catch (Throwable t) { throw new Error(t); } } } With this code, your library doesn’t need to know about the implementations of the service. Users of your library … Read more

Why are readObject and writeObject private, and why would I write transient variables explicitly?

(1) The methods are not declared in any class or interface. A class, that implements the Serializable interface and requires special special handling during the serialization and deserialization process must implement those methods and the serializer/deserializer will try to reflect those methods. This is one of the rather strange corners in Java where the API … Read more

What is an AssertionError? In which case should I throw it from my own code?

Of course the “You shall not instantiate an item of this class” statement has been violated, but if this is the logic behind that, then we should all throw AssertionErrors everywhere, and that is obviously not what happens. The code isn’t saying the user shouldn’t call the zero-args constructor. The assertion is there to say … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)