Re: Java Interface usage



On 11 Jul 2005 20:19:52 -0700, Rod wrote:
> I recently saw some code that looked like the example below .. my
> question is why the developer used "this(SOME_WORDS)" in the
> constructor for SomeClass.
>
> Seems to me that the static string array SOME_WORDS would have been
> available to SomeClass without this code in the constructor ....
>
>========================================================
> public interface SomeWords{
>
> static final String SOME_WORDS[] = {
> "Tom",
> "***",
> "Harry"
> };
> }
>
> public class SomeClass implements SomeWords {
>
> public SomeClass() {
> this(SOME_WORDS);
> }

Yes, the code could refer directly to SOME_WORDS anyway, but then any
application using this class is locked in to a single word list, and
the class isn't nearly as useful or flexible as it could be. In order
to use a different word list, you'd need to recompile the SomeWords
interface.

This constructor is obviously missing from your example:

public SomeClass(String[] words) {
/* ... */
}

So the default constructor (the one you showed) initializes the class
with the given wordlist, and maybe that is all that's needed in most
cases. However an application needing different words can do something
like this:

String[] my_words = WordFileReader.readWordsFrom("special_words.txt");
SomeClass sc = new SomeClass(my_words);

/gordon

--
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
.