java - Is using enums efficient for many constants memory-wise? -


i have class looks this:

public class messagebuilder{    private enum msgcodes{      code_1("some string"),     code_2("another string"),     code_3("you idea");      private string msg;      msgcodes(string msg){       this.msg = msg;     }      private string text(){       return msg;     }   }    private messagebuilder(){     //prevents initialization outside class   }    //gives synchronized behaviour initialization without enforcing getinstance()   private static class loader{     static messagebuilder instance = new messagebuilder();   }    public static messagebuilder getinstance(){     return loader.instance;   }    public string buildmessage(string[] codes){     string res = "";     for(string code : codes){       res += qamsg.valueof(code).text();     }     return res;   }  } 

my concern overtime (meaning application develops) have more , more enum on class (which understand not preferred way keep constants used on 1 class), i'm rather new enum don't know happen if list becomes "too big" still efficient way keep them?

is there different approach use not entire list 1 code i'm using @ time gets instantiated? enum instances 1 time or instancing every time use them?

i made class singleton thinking prevent me having enum list instantiated more once, might unnecessary don't understand enum behaviour.

enum members public static final constants, therefore singletons. let's suppose list grows 10,000 enum members , each costs 1 kb. under these extravagant assumptions amount 10 mb of java heap.

so clearly, should not worry memory consumption of enum members.


Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -