1

This explains how to do it when you know the names for the items at compile time, but what if I want them defined at runtime... i.e. Today, Tomorrow, (Day and Date after Tomorrow), etc...

finiteloop
  • 4,444
  • 8
  • 41
  • 64
  • 2
    what's the difficulty? why can't you create array in run-time and fill it with desired items? – woodshy Jul 04 '11 at 21:09
  • The items variable has to be declared as final. Am I missing something? Doesn't final mean that I can't change the value? – finiteloop Jul 04 '11 at 21:45

1 Answers1

0

The items variable has to be declared as final.

No, it doesn't.

Doesn't final mean that I can't change the value?

For that specific example, it is declared final because it is shown as being just a local variable, and the value is also needed inside the anonymous DialogInterface.OnClickListener inner class.

However:

  1. Just because something is declared final does not mean it has to be a literal. For example, the final keyword is used on parameters to methods sometimes, and those clearly are not statically created. To quote Wikipedia, "A final variable can only be initialized once, either via an initializer or an assignment statement."

  2. There are other places you can put your array other than a local variable that will not require the final keyword, if you do not wish to use final. One likely candidate would be a data member of your activity.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Ah. Great. Thanks for the help. I just read up some more on final, and realize that my understanding was way lacking. Coming from C++, I don't always realize when stuff is drastically different in java until after posting on here. Thanks again. – finiteloop Jul 05 '11 at 17:30
  • 1
    @segfault: Yeah, you'll run into the occasional terminology overlap there. I seem to recall running into that problem when I learned Java, back when dinosaurs roamed the Earth. Of course, I learned C++ years before that, from some well-organized amino acids in the primordial soup. (mmmmmmmmm... soup!) :-) – CommonsWare Jul 05 '11 at 17:42