Link to home
Start Free TrialLog in
Avatar of renisenbb
renisenbb

asked on

Java: Volatile variable

In a book i'm reading, it shows below code and states: if 'data' isn't defined as a volatile var, other classes might access it before it's completely initialized.  Can someone explain this statement to me. There's nothing else written about it.
class StockData {}
class StockExchange {
      static volatile StockData data = null;
      static void loadStockData () {
            //load and initialize reference variable data. the variable data is written to only once
      }
}
class BuyStocks {
      public void buy () {
            if (StockExchange.data != null) {
                     //analyze data
                    //buy stock
            }
      }
}
SOLUTION
Avatar of krakatoa
krakatoa
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial