The Source for Java Technology Collaboration

Home » java.net Forums » JDK » Java SE

Thread: private static final variable issue

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is not answered. Helpful answers available: 2. Correct answers available: 1.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 7 - Last Post: Jan 7, 2009 5:50 PM by: interact Threads: [ Previous | Next ]
prashu_javanet

Posts: 3
private static final variable issue
Posted: Jan 6, 2009 9:54 PM
 
  Click to reply to this thread Reply

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

peter__lawrey

Posts: 188
Re: private static final variable issue
Posted: Jan 7, 2009 2:21 AM   in response to: prashu_javanet
 
  Click to reply to this thread Reply

Replace CURRENT_YEAR with a function which returns the current year.

phenderson

Posts: 86
Re: private static final variable issue
Posted: Jan 7, 2009 3:15 AM   in response to: peter__lawrey
 
  Click to reply to this thread Reply

I agree with that.

Once your code has been fixed, fire the dope who wrote the original version.

anthavio

Posts: 9
Re: private static final variable issue
Posted: Jan 7, 2009 3:53 AM   in response to: prashu_javanet
 
  Click to reply to this thread Reply

private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);

anthavio

Posts: 9
Re: private static final variable issue
Posted: Jan 7, 2009 3:56 AM   in response to: anthavio
 
  Click to reply to this thread Reply

Well, application restart is still needs to reinit static final. Shame on me! Function is really needed.

saimatam

Posts: 3
Re: private static final variable issue
Posted: Jan 7, 2009 9:48 AM   in response to: prashu_javanet
 
  Click to reply to this thread Reply

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.

saimatam

Posts: 3
Re: private static final variable issue
Posted: Jan 7, 2009 9:48 AM   in response to: prashu_javanet
 
  Click to reply to this thread Reply

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.

interact

Posts: 1
Re: private static final variable issue
Posted: Jan 7, 2009 5:50 PM   in response to: saimatam
 
  Click to reply to this thread Reply

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;
    }
}





 XML java.net RSS