1. Ordinary full-Han style, a hungry man style
Package Org.bighead.test2;public class Testprivate {private String str = "Strprivate";p rivate static final testprivate TES Tprivate = new Testprivate ();//Full-han, a hungry man-style private testprivate () {super (); System.out.println (str);} Public Testprivate getinstance () {return testprivate;} public void Toprint () {System.out.println ("private construct-open method-Single instance");}}
Reflection creates an instance, invokes a method (an instance is accessible)
Package Org.bighead.test2;import Java.lang.reflect.constructor;import Java.lang.reflect.InvocationTargetException ;p Ublic class testdefaultsub {public static void main (string[] args) {class<testprivate> clazz = Testprivate.class;try {constructor<?> Constructor = Clazz.getdeclaredconstructor (); constructor.setaccessible (true); testprivate TP = (testprivate) constructor.newinstance (); Tp.toprint ();} catch (Nosuchmethodexception e) {e.printstacktrace ();} catch (Instantiationexception E1) {e1.printstacktrace ();} catch (Illegalaccessexception E1) {E1.printstacktrace ();} catch (InvocationTargetException E1) {e1.printstacktrace ();}}}
2. Concurrency can also affect
Java Reflection (Java.lang.reflect)---Common singleton mode uniqueness problem