1

I was going through the example, in this link, I wonder how the class can access the private variable lock, in the code sample. Kindly explain me pros !!. The code is here at, yourLock = bower.lock.tryLock();.

public class Safelock {
static class Friend {
    private final String name;
    private final Lock lock = new ReentrantLock();

    public Friend(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    public boolean impendingBow(Friend bower) {
        Boolean myLock = false;
        Boolean yourLock = false;
        try {
            myLock = lock.tryLock();
            yourLock = bower.lock.tryLock();
        } finally {
            if (! (myLock && yourLock)) {
                if (myLock) {
                    lock.unlock();
                }
                if (yourLock) {
                    bower.lock.unlock();
                }
            }
        }
        return myLock && yourLock;
    }
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Deepan Prabhu Babu
  • 862
  • 11
  • 18
  • 3
    It's the same `class`. There's no privacy issue here. A `class` can always access its own members... (for more information see every `equals` implementation ever) – Boris the Spider Dec 16 '16 at 14:37
  • duplicate question from http://stackoverflow.com/questions/1801718/why-can-outer-java-classes-access-inner-class-private-members – Ran Koretzki Dec 16 '16 at 14:42

0 Answers0