title: Alternative to Fixed-Size Arrays in Java?
body: <p>[<em>Context: new to Java, 4 months tops; old hand at C++.</em>]</p>
  
  <p>I'm working on a library that requires an array of a fixed size ("fixed string") in many places. I'm trying to use Dependency Injection (q.v.) for this particular problem, so I would like something of the form:</p>
  
  <pre><code>class Foo 
  {
      private Bar injectedBar;
      private char[] injectedFixedString;
  
      Foo(Bar injectedBar, /* what can go here? */ char[5] injectedFixedString);
      { /* initializing code goes here /* }
  }
  </code></pre>
  
  <p>Simple is required -- this is going into an auto-generated communications protocol. I have zero control of the protocol and database it's being derived from; I will have hundreds, if not thousands, of these instances in the final code. So, given all that:</p>
  
  <p>Is my only alternative to the C++:</p>
  
  <pre><code>char injectedFixedString[5];
  </code></pre>
  
  <p>to create a custom class? Something like:</p>
  
  <pre><code>class FixedBarString {
      /* could also set this in the constructor, but this complicates code generation a tad */
      public static integer STRING_SIZE = 5; /* string size */
      char[] fixedString = new char[STRING_SIZE];
  
      FixedBarString(char[] string) throws RuntimeException {
        /* check the string here; throw an exception if it's the wrong size.
           I don't like constructors that throw however. */
      }
  
      public void setString(char[] string) throws RuntimeException {
        /* check the string here */
      }
  
      public char[] getString() {
        /* this isn't actually safe, aka immutable, without returning clone */
      }
  
      public char[] createBlankString() {
        return new char[STRING_SIZE];
      }
  }
  </code></pre>
  
  <p>Thanks. (My apologies if this is too much code).</p>