find bug error static field from instance method is risky.

Write to static field from instance method

This instance method writes to a static field. This is tricky to get correct if multiple instances are being manipulated, and generally bad practice.

public static loadingDialog StatusWndw = null;

StatusWndw = new loadingDialog(this);i want to acess this ref outside the class.

[353 byte] By [lkkja] at [2007-11-27 11:03:40]
# 1

This makes no sense to me. Explain further what your intentions are.

dwga at 2007-7-29 12:51:37 > top of Java-index,Java Essentials,Java Programming...
# 2

hi,

i am assinig a static refence to an obect, and acess this refrence outside the class.

public static loadingDialog StatusWndw = null;

StatusWndw = new loadingDialog(this);

i want toacess StatusWndw this out side the class but find bug show me generally bad practice.

pls reply.

regards,

lkkj

lkkja at 2007-7-29 12:51:37 > top of Java-index,Java Essentials,Java Programming...
# 3

Why is it that you want to do that?

What would happen if that happened in more than one place in your program?

Might a static method help? The following code is not tested.

public class AClass

static LoadingDialog statusWindow;

public static void makeLoadingDialog(SomeClass who)

{ statusWindow = new LoadingDialog(who);

}

}

Or maybe this just covers up the real problem that FindBugs is pointing to. I'm not sure why it says it's bad practice, though I get a feeling that it's correct in stating it may be difficult to get correct.

Your code would be more readable if you used the convention of capitalized class names (LoadingDialog) and non-capitalized variable names (statusWindow).

OleVVa at 2007-7-29 12:51:37 > top of Java-index,Java Essentials,Java Programming...