Please explain to me the strange behavior of the variable. From the main thread creates an instance of the class "B". From the constructor of the parent "A" is called an abstract function "init" of the class "B". It initializes a debugPaint member of the class "B".
Then, it creates a Thread which periodically calls the function postDraw. The problem is that if I assign private volatile Paint debugPaint=null function postDraw receive debugPaint member as null. Although as I can see in the debugger initialization was successful previously. If the assignment to null is not done, then everything works. private volatile Paint debugPaint; What is the problem?
p.s Time between init and postDraw is a lot for a few seconds.
public class A{
public A()
{
init();
}
public void draw(Canvas canvas)
{
//some code....
postDraw(canvas);
}
abstract public void postDraw(Canvas canvas);
abstract public void init();
}
public class B extends A{
private volatile Paint debugPaint=null;//=null problem! not =null ok!
@Override
public void init()
{
debugPaint=new Paint();
}
@Override
public void postDraw(Canvas canvas)
{
canvas.drawRect(0,0,128,128,debugPaint);
}
}