I need to maintain a number of dates in my program and I'm doing that in the form of an array of Calendar objects.
To access it from several other classes, I plan to declare it in a separate class and import the array as a static import.
Now how can I set values for each of those objects? I'm able to initialize them and access them from the other class like this :
public static Calendar[] cal = new Calendar[]{
Calendar.getInstance(),
Calendar.getInstance()};
Now how do I set values to them?
I tried Calendar.getInstance().set(2012, 11, 10, 16, 30)
but i get an error with no suggestions.
So,
- Is static import the correct (or best) way to do this?
- How do I set values to it?
The static import part is irrelevant, although frankly it's not terribly nice having a publicly accessible writable field of a mutable array of mutable objects (calendars). A better approach would be to expose an
ImmutableList<DateTime>
using Guava forImmutableList
and Joda Time forDateTime
. (Or possiblyLocalDateTime
) Anyway...You could use a static initializer block, but I'd probably just write a method instead: