The closest would be to make PrivateObject an inner class of Box and make increment
private. Thus, the method will be available only in the Box
class.
public class Box { private PrivateObject prv; public void setPrivateObject(PrivateObject p) { prv = p; } public void changeValue() { prv.increment(); } public static class PrivateObject { private int value; private void increment() { value++; } } }
If you do not want to do this, the next option would be to make a private increment
package and find 2 classes in one package. Thus, only classes inside this package will have access to this method. But this may include classes other than Box.
source share