Home » Tip Bank » Java » Classes
  • Comment on this Article
Language: Java
Expertise: Intermediate
Nov 8, 2019

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Watch

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();
Octavia Anghel
 
Submit a Tip
Browse "Java" Tips
Browse All Tips
htmlgoodies.com
googlecode.com