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
}
}
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.