I have a class like this
public class StockCodes
{
public static boolean loaded = false;
public static long lastUpdate = 0;
private static long freq = 1000 * 60 * 60 * 24;
public static HashMap<String, Stock> stockMap = new HashMap<String, Stock>();
public static ArrayList<Stock> getCodes()
{
long now = System.currentTimeMillis();
if ((StockCodes.loaded) && ((now - StockCodes.lastUpdate) < freq))
{
System.out.println(StockCodes.loaded);
return stockList;
}
else
{
HttpFetcher fetcher = new HttpFetcher();
....
log.info("Load " + stockList.size() + " Stocks");
StockCodes.loaded = true;
StockCodes.lastUpdate = now;
return stockList;
}
}
public static void main(String[] args)
{
StockCodes.getCodes();
}
}
When I run the java application , it outputs “true”. Telling us the stockcodes have loaded.
But when i debug the java appliction, it goes into the else. I do not understand why it runs in this way, especially why it outputs “true” the first time i run the application. Could someone else give me some help ,thank you ~
With the code given above, the control by default should always go in the
elseblock and print nothing. You can verify it.Not if you have some more places where you call the
getCodes()method, then it will differ. Having said this, please verify if there is no other place with a call togetCodes(). ie its invoked single time viamain()