Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Here is a small trick that I use in JAVA to replicate C++ friend mechanism.</strong></p> <p>Lets say I have a class <code>Romeo</code> and another class <code>Juliet</code>. They are in different packages (family) for hatred reasons.</p> <p><code>Romeo</code> wants to <code>cuddle</code> <code>Juliet</code> and <code>Juliet</code> wants to only let <code>Romeo</code> <code>cuddle</code> her.</p> <p>In C++, <code>Juliet</code> would declare <code>Romeo</code> as a (lover) <code>friend</code> but there are no such things in java.</p> <p>Here are the classes and the trick :</p> <p>Ladies first :</p> <pre class="lang-java prettyprint-override"><code>package capulet; import montague.Romeo; public class Juliet { public static void cuddle(Romeo.Love l) { l.hashCode(); System.out.println("O Romeo, Romeo, wherefore art thou Romeo?"); } } </code></pre> <p>So the method <code>Juliet.cuddle</code> is <code>public</code> but you need a <code>Romeo.Love</code> to call it. It uses this <code>Romeo.Love</code> as a "signature security" to ensure that only <code>Romeo</code> can call this method and simply calls <code>hashCode</code> on it so the runtime will throw a <code>NullPointerException</code> if it is <code>null</code>.</p> <p>Now boys :</p> <pre class="lang-java prettyprint-override"><code>package montague; import capulet.Juliet; public class Romeo { public static final class Love { private Love() {} } private static final Love love = new Love(); public static void cuddleJuliet() { Juliet.cuddle(love); } } </code></pre> <p>The class <code>Romeo.Love</code> is public, but its constructor is <code>private</code>. Therefore anyone can see it, but only <code>Romeo</code> can construct it. I use a static reference so the <code>Romeo.Love</code> that is never used is only constructed once and does not impact optimization.</p> <p>Therefore, <code>Romeo</code> can <code>cuddle</code> <code>Juliet</code> and only he can because only he can construct and access a <code>Romeo.Love</code> instance, which is required by <code>Juliet</code> to <code>cuddle</code> her (or else she'll slap you with a <code>NullPointerException</code>).</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload