[Context: new to Java, 4 months tops; old hand at C++.]
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:
class Foo
{
private Bar injectedBar;
private char[] injectedFixedString;
Foo(Bar injectedBar, /* what can go here? */ char[5] injectedFixedString);
{ /* initializing code goes here /* }
}
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:
Is my only alternative to the C++:
char injectedFixedString[5];
to create a custom class? Something like:
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];
}
}
Thanks. (My apologies if this is too much code).