Single Case Mode Static Internal Class Resolves Reflection to Get Multiple Objects

Keywords: Java jvm

First of all, there is no single pattern of hungry Han, lazy, if you need Baidu


A little better than the Hungry Han style is the static inner class singleton

Paste code here

package com.test;

public class SimpleTest {
	
	private static class SimpleNB {
		
		private static final SimpleTest INSTANCE = new SimpleTest();
	}

	private SimpleTest() {

	}

	public static SimpleTest getIns() {
		return SimpleNB.INSTANCE;
	}
}
But there's still a way for me to reflect to get his construction, change it to public, and newInstance() creates the object

Paste code here

package com.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

/**
 * @date 2017-10-20
 * @author xgf
 *
 */
public class Test {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {

		
		Constructor<SimpleTest> declaredConstructor = SimpleTest.class.getDeclaredConstructor();
		declaredConstructor.setAccessible(true);
		SimpleTest newInstance1 =declaredConstructor.newInstance();
		SimpleTest newInstance2 =declaredConstructor.newInstance();
		System.out.println(newInstance1.equals(newInstance2));
	}
}
The result here is false

Post a picture




Then, based on this problem, come up with a solution to use enumeration because jvm is enumerated and constructor visibility is immutable

Paste code here

package com.test;




public enum Singleton6
{
    INSTANCE;
    
    private SimpleTest instance;
    
    Singleton6()
    {
        instance = SimpleTest.getIns();
    }
    
    public SimpleTest getInstance()
    {
        return instance;
    }
    
    
}
Then the test code
package com.test;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;

/**
 * @date 2017-10-20
 * @author xgf
 *
 */
public class Test {
	public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Constructor<Singleton6> declaredConstructor = Singleton6.class.getDeclaredConstructor();
		declaredConstructor.setAccessible(true);
		Singleton6 newInstance1 =declaredConstructor.newInstance();
		Singleton6 newInstance2 =declaredConstructor.newInstance();
		System.out.println(newInstance1.equals(newInstance2));
//		Singleton6 instance1 = Singleton6.INSTANCE;
//		Singleton6 instance2 = Singleton6.INSTANCE;
//		System.out.println(instance1.equals(instance2));
		
//		Constructor<SimpleTest> declaredConstructor = SimpleTest.class.getDeclaredConstructor();
//		declaredConstructor.setAccessible(true);
//		SimpleTest newInstance1 =declaredConstructor.newInstance();
//		SimpleTest newInstance2 =declaredConstructor.newInstance();
//		System.out.println(newInstance1.equals(newInstance2));
	}
}

Then paste out the console output


At this point, you should understand the basic point. When you see people here, I just want you to share more (whether it's technology or anything else) and be grateful to your predecessors.What's wrong? Please comment or send me a private message. Thank you


Posted by sajlent on Tue, 14 Jul 2020 07:26:20 -0700