java - Advantages/Disadvantages of code location when accessing Resource file -


here 3 options have seen when accessing resource file. option 1 least recommended due potential of exceptions question pertains option 2 or 3 preferred or recommended implementation.

option 1 - done in attributes area. generic. doesn't capture potential exceptions.

class myclass  {     static resourcebundle bundle = resource.getbundle("myfile");     float value1 = float.parsefloat(bundle.getstring("myvalue1"));     float value2 = float.parsefloat(bundle.getstring("myvalue2")); } 

option 2 - access resources within constructor. since values won't dynamic seems waste access them every time class instantiated class heavily used item.

class myclass  {     static resourcebundle bundle = resource.getbundle("myfile");     float value1;      float value2;      public myclass()     {         try         {             value1; = float.parsefloat(bundle.getstring("myvalue1"));             value2 = float.parsefloat(bundle.getstring("myvalue2"));         }catch(exception e)         {             //do         }     } } 

option 3 - code within attributes section of class. accessed once if class in memory, since attributes tend @ top of class, make code appear cluttered try/catch , code.

class myclass  {     static resourcebundle bundle = resource.getbundle("myfile");     float value1;      float value2;     {         try         {             value1; = float.parsefloat(bundle.getstring("myvalue1"));             value2 = float.parsefloat(bundle.getstring("myvalue2"));         }catch(exception e)         {             //do         }     } } 

seems more matter of preference else don't expect overhead difference measurable if there 20-30 resources being accessed.

your option 2 , 3 produce same bytecode. difference aesthetical.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -