What is Law of Demeter?
<p>Let's start with Wikipedia:</p>

<blockquote>
  <p>More formally, the Law of Demeter for functions requires that a method <strong>m</strong> of an object <strong>O</strong> may only invoke the methods of the following kinds of objects:</p>
  
  <ol>
  <li>O itself</li>
  <li>m's parameters</li>
  <li>Any objects created/instantiated within m</li>
  <li>O's direct component objects</li>
  <li>A global variable, accessible by O, in the scope of m</li>
  </ol>
</blockquote>

<p>Rule 1:</p>

<pre><code>public class ClassOne {

    public void method1() {
        method2();
    }

    public void method2() {

    }
}
</code></pre>

<p>Rule 2:</p>

<pre><code>public class ClassOne {

    public void method1(ClassTwo classTwo) {
        classTwo.method2();
    }
}

class ClassTwo {

    public void method2() {

    }
}
</code></pre>

<p>Rule 3:  </p>

<pre><code>public class ClassOne {

    public void method1() {
        ClassTwo classTwo = new ClassTwo();
        classTwo.method2();
    }
}

class ClassTwo {

    public void method2() {

    }
}
</code></pre>

<p>Rule 4 (thanks @juharr):</p>

<pre><code>public class ClassOne {

    private ClassTwo classTwo;

    public void method1() {
        classTwo = new ClassTwo();
        classTwo.method2();
    }
}

class ClassTwo {

    public void method2() {

    }
}
</code></pre>

<p>Rule 5:</p>

<pre><code>?
</code></pre>

<p>Can anyone help me with Rule 5?</p>

<hr>

<p>And doesn't Law of Demeter imply that chaining is bad?</p>

<pre><code>User.getName().getLastName();
</code></pre>

<p>This leads to high coupling.</p>

<hr>

<p>Isn't "Tell, don't ask" a similar principle? </p>

<p>So is this everything? Am I wrong about something? How can you obey Law of Demeter? </p>