java - Incentives for or against private static final immutables on @service in spring -


i'm new programming in area of professional software development, , collegues had discussion using fields following in spring @service annotated class:

private static final map<key, value> somemap; static {     map<key, value> map = new hashmap<>();     map.put(akey, avalue);     somemap = collections.unmodifiablemap(map); } 

can leave out keywords without compromising safety/features under following premises:

  • the map never modified in way except directly programmer access source code
  • the map not made available classes outside
  • the class containing map annotated service default scope springboot
  • edit assume both key , value enum values without other information or functionality in them.
  • edit map intended better-maintainable alternative method containing switch case statement
  • edit map in no way made accesible other class

what advantages or disadvantages of approach on using this:

private final map<key, value> somemap;  public myclass() {     map<key, value> map = new hashmap<>();     map.put(akey, avalue);     somemap = collections.unmodifiablemap(map); } 

the main question should ask how many instances of class going created.

one class instance

the default scope of @service singleton there isn't difference whether create map static member or regular one. there 1 instance of map , difference in moment when initialization happens. singleton scope there no real reason use static members.

however, creating map regular member of class may avoid problem static members in future. instance, if ever unit test class , stub map testing scenario, more problematic static member. static members can't benefit dependency injection , @value injection in case need it.

many class instances

if change default scope of @service class , share private internal state instances using static member might reasonable choice in cases. must remember map can accessed or modified instances concurrently, hence should make sure design class thread safety in mind.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -