title: How to "public class FoobarID extends Long"?
body: <p>I guess I have to accept that <code>Long</code> is <code>final</code> and cannot be extended.</p>
  
  <p>In our project we use a database and pass IDs for different things around in the code as <code>Long</code>-s.</p>
  
  <p>But a <code>UserID</code> is not a <code>LogID</code> and is not a <code>ServerID</code> although they're all <code>Long</code>s. It seems to me the compiler and IDE should be able to detect a whole category of bugs e.g. when trying to use a <code>UserID</code> in place of a <code>ServerID</code>. Currently the compiler doesn't detect that, because the type of both is <code>Long</code>.</p>
  
  <p>If <code>Long</code> was not <code>final</code> I could say</p>
  
  <pre><code>public class UserID extends Long {};
  </code></pre>
  
  <p>but <code>Long</code> is <code>final</code>, so I can't. Is there something more elegant than:</p>
  
  <pre><code>    public class UserID {
          private Long ID;
          UserID(Long ID) {
              this.ID = ID;
          }
          UserID(UserID ID) {
              this.ID = ID.toLong();
          }
          public Long toLong() {
              return ID;
          }
          public void fromLong(Long ID) {
              this.ID = ID;
          }
      };
  </code></pre>
  
  <p>Edit: I <em>know</em> that this is inelegant - "bad"! The comments seem to assume I'm about to put something like this in my production code. I'm not.</p>
  
  <p>This just seems so wasteful and inelegant. And I guess good luck getting e.g. <a href="https://commons.apache.org/proper/commons-dbutils/" rel="nofollow">DbUtils</a> to work with <code>UserID</code>s in its JavaBean handling.</p>
  
  <p>Is there "a better way" (TM)? ( Other than just living with the <code>Long</code>-s ;-) )</p>