java - For what types variables must never be static? -
in 1 library in heavy use in our project there restriction variables of classes must never static. (it ulc). far understood because of need serialize of them. , problem rule, is not strict , may cause of bugs hard debug.
we going write module checkstyle detect static variables of such types (detected customizable regexp probably). , need know how necessary check other developers.
so question is: general circumstances when variables of types must never static?
first, proper object oriented design should inform decision make method/field static.
second, in web application, requests handled on separate threads, have careful how use static methods/fields. if static method maintains state across invocations(by using static field keep count, example), can run threading issues. happens because 1 request might invoke static method, , stopped in middle of execution thread invokes method. if first invocation modified common resource, did not finish, second invocation might corrupt progress of first execution.
Comments
Post a Comment