devxlogo

Call a Private Constructor

Call a Private Constructor

Calling a private constructor from outside its class can be done via Reflection as follows:

public final class Users {   private Users() {}  // static members}Class usersClass = Users.class;Constructor emptyUsersCnstr = usersClass.getDeclaredConstructor();emptyUsersCnstr.setAccessible(true);Users usersViaEmptyCnstr = emptyUsersCnstr.newInstance();

devx-admin

Share the Post: