java - Should static methods be separated from Classes with instance methods? -
as general convention, should static method(s) separated class class instance methods?
is there example of reason?
there no general convention dictates static method must separate non-static method. in fact, if 2 methods related enough 1 another, counter-intuitive have methods separated.
recall use case static methods (and fields) have: they're methods/fields can used without instance of particular class. means hold valuable metadata or perform useful operation that's related class instance, not require direct instantiation of class.
take, example, integer
. has static [final
] fields max_value
, min_value
. since both of these fields contain fixed information not change between instantiations, not make sense have instantiate integer
information.
integer
has useful operation parseint
, takes string
, turns int
. shouldn't require instance of integer
convert string
int
, especially if we're not placing instance of integer
.
the overarching convention has been keep related methods together, regardless of if they're static or not. can see clearer examples of in java library classes, integer
.
Comments
Post a Comment