In what situations is static method a good practice?

后端 未结 11 1058
走了就别回头了
走了就别回头了 2020-12-04 10:04

I have read the following discussions:

Should private helper methods be static if they can be static , and
Should all methods be static if their class has no mem

11条回答
  •  忘掉有多难
    2020-12-04 10:53

    I often use static factory methods instead of or in conjunction with public constructors.

    I do this, when i need a constructor that does something you would not expect a constructor to do. I.e. load settings from a file or database.

    This approach also provides the possibility of naming the "constructors", based on what they do. This is especially useful, when the parameters themselves are not enough to figure out what happens in a constructor.

    Sun uses this approach in

    Class.forName("java.lang.Integer");
    

    and

    Boolean.valueOf("true");
    

提交回复
热议问题