Matthew Murdoch Matthew Murdoch - 2 years ago 117
Java Question

Is there a way to simulate the C++ 'friend' concept in Java?

I would like to be able to write a Java class in one package which can access non-public methods of a class in another package without having to make it a subclass of the other class. Is this possible?

Answer Source

The 'friend' concept is useful in Java, for example, to separate an API from its implementation. It is common for implementation classes to need access to API class internals but these should not be exposed to API clients. This can be achieved using the 'Friend Accessor' pattern as detailed below:

The class exposed through the API:

package api;

public final class Exposed {
    static {
        // Declare classes in the implementation package as 'friends'
        Accessor.setInstance(new AccessorImpl());

    // Only accessible by 'friend' classes.
    Exposed() {


    // Only accessible by 'friend' classes.
    void sayHello() {

    static final class AccessorImpl extends Accessor {
        protected Exposed createExposed() {
            return new Exposed();

        protected void sayHello(Exposed exposed) {

The class providing the 'friend' functionality:

package impl;

public abstract class Accessor {

    private static Accessor instance;

    static Accessor getInstance() {
        Accessor a = instance;
        if (a != null) {
            return a;

        return createInstance();

    private static Accessor createInstance() {
        try {
            Class.forName(Exposed.class.getName(), true, 
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException(e);

        return instance;

    public static void setInstance(Accessor accessor) {
        if (instance != null) {
            throw new IllegalStateException(
                "Accessor instance already set");

        instance = accessor;

    protected abstract Exposed createExposed();

    protected abstract void sayHello(Exposed exposed);

Example access from a class in the 'friend' implementation package:

package impl;

public final class FriendlyAccessExample {
    public static void main(String[] args) {
        Accessor accessor = Accessor.getInstance();
        Exposed exposed = accessor.createExposed();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download