|
Replies:
7
-
Last Post:
Jan 7, 2009 5:50 PM
by: interact
|
Threads:
[
Previous
|
Next
]
|
|
|
|
|
|
private static final variable issue
Posted:
Jan 6, 2009 9:54 PM
|
|
|
Hi guys,
I came across one intersting bug in our application.
We had declared one variable as private static final CURRENT_YEAR which holds the current year when the class is loaded. The application is up and runnning from last year 2008 but, when it changed to new year 2009, still the value of this variable was 2008. We had to undeploy and redeploy the application to take new year.
How to tackle this kind of problems?Please advice.
Thanks in advance.
Thanks, Prashant
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 2:21 AM
in response to: prashu_javanet
|
|
|
Replace CURRENT_YEAR with a function which returns the current year.
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 3:15 AM
in response to: peter__lawrey
|
|
|
I agree with that.
Once your code has been fixed, fire the dope who wrote the original version.
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 3:53 AM
in response to: prashu_javanet
|
|
|
private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 3:56 AM
in response to: anthavio
|
|
|
Well, application restart is still needs to reinit static final. Shame on me! Function is really needed.
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 9:48 AM
in response to: prashu_javanet
|
|
|
CURRENT_YEAR feels like a constant but it is not ! It changes it value - although very slowly!
Having a method getCurrentYear() is the way to go.
In extreme cases, where performance is very very critical - you can come up with sophisticated caching schemes where you expire the cached value every second or so.
--Sai Matam.
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 9:48 AM
in response to: prashu_javanet
|
|
|
CURRENT_YEAR feels like a constant but it is not ! It changes it value - although very slowly!
Having a method getCurrentYear() is the way to go.
In extreme cases, where performance is very very critical - you can come up with sophisticated caching schemes where you expire the cached value every second or so.
--Sai Matam.
|
|
|
|
|
|
|
|
Re: private static final variable issue
Posted:
Jan 7, 2009 5:50 PM
in response to: saimatam
|
|
|
If you don't want to instantiate a calendar object each time the method is called, you could keep track of the expiration time of the current year as demonstrated below.
public class CurrentYear
{
public static void main( String[] args )
{
System.out.println( "Current Year is " + getCurrentYear() );
}
private static int currentYear = 0;
private static long expireCurrentYear = 0;
public static int getCurrentYear()
{
// not initialized or expired, obtain a new current year
if ( currentYear == 0 || System.currentTimeMillis() >= expireCurrentYear )
{
Calendar cal = Calendar.getInstance();
currentYear = cal.get( Calendar.YEAR );
// set cal to jan/1 of next year 00:00:00.0000, which will be
// used as expiration time for the current year
cal.set( Calendar.DAY_OF_MONTH, 1 );
cal.set( Calendar.MONTH, 0 );
cal.set( Calendar.HOUR_OF_DAY, 0 );
cal.set( Calendar.MINUTE, 0 );
cal.set( Calendar.SECOND, 0 );
cal.set( Calendar.MILLISECOND, 0 );
cal.add( Calendar.YEAR, 1 );
expireCurrentYear = cal.getTimeInMillis();
}
return currentYear;
}
}
|
|
|
|
|