For collection of smaller helper utility classes, I have created a general class MyUtils:
// MyUtils.java
public final class MyUtils
{
public static class Helper1 {};
public static class Helper2 {};
//...
}
This helper classes from inside MyUtils will be used in the other files of the package:
// MyClass1.java
public class MyClass1
{
private MyUtils.Helper1 help1 = new MyUtils.Helper1();
public void method ()
{
private MyUtils.Helper2 help2 = new MyUtils.Helper2();
}
}
To let them accessible, I have made them static inside MyUtils (which doesn’t have any data/function member of its own). My code is thread safe before creating MyUtils.
My worry is, by making these inner classes staticwill they remain thread safe, when their multiple instances will exist across the files ? Or is their any bad implication am I missing due to making them static ?
Edit: I am not touching any shared variable inside the helper classes. My only concern was that will the instance of the static classes be thread safe (since they are static).
If you’re asking whether these is any bad implication of going from:
…to:
Then no, there is not. The
statickeyword in this case is just “promoting” the nested inner class to a top-level class, so that you can instantiate it without needing an enclosing instance ofMyUtils. Here is a passable article on the subject:http://www.javaworld.com/javaworld/javaqa/1999-08/01-qa-static2.html
In essence, doing
public static class Xon a nested inner-class is the same as doingpublic class Xin a standard top-level class.