make class only instantiable from specific class
问题 Let's say I have 3 classes class1 , class2 and class3 . How can I have it that class1 can only get instantiated by class2 ( class1 object = new class1() ) but not by class3 or any other class? I think it should work with modifiers but I am not sure. 回答1: I want to rename your classes to Friend , Shy and Stranger . The Friend should be able to create Shy , but Stranger should not be able to. This code will compile: package com.sandbox; public class Friend { public void createShy() { Shy shy =