i need to write an ajax applicatoin, where on the server side (implemented in java) millions of small objects will be created and held in memory. normally I use the beanstyle (with getter / setter) for any objects.
My question: Does it make a difference (in regard to memory usage and CPU Time for access) at runtime, having getter/setter methods for an instanciated object versus not having them and directly accessing the fields/members? For example if having getter/setter makes an object bigger for 5bytes per method and instance, then this would dramatically add up in total with millions ob objects. The same with accessing a getter vers directly accesing the member/variable in regard to cpu cycles.
public String myvartoaccess;
vs
private String myvartoaccces;
public String getMyvartoaccess();
Thanks very much!
Jens
Getters and setters do not affect memory usage. The method implementations of getter/setter are stored with the
Classof which there is only one (in a given classloader). The instances only store the state of the object.Regarding performance, I doubt it would be significantly slower to call simple getter/setters. I would definitely first implement with the getters and setters, and then if you find it is too slow, you could potentially do away with them. Premature optimization is the root of all evil after all.