Considering the following toy example class:
public class Test {
private volatile Outer outerVar = new Outer();
static class Outer {
Inner innerVar = new Inner();
}
static class Inner {
// state
// setters
// getters
}
private void multithreadedUse() {
// play with outerVar.innerVar
}
}
outerVar is volatile so all threads that may be using it will see it in the same state. But what about outerVar.innerVar? Does the fact that its parent object ( outerVar ) is marked as volatile make it volatile also?
Or do we have to declare innerVar volatile explicitly?