当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Alternative to static arraylist, for storing data and to be accessed in other package

问题描述:

I am working on a project, in which i have a list that is populated whenever service is being called. This list is in the Service Adapter class, which is being called whenever a service is being called. So now I have to access this list in another package, without making it static. I am requesting a server for the data through the service calls

Rightnow I have made this list static, and have a public class in the same package, with static getter and setter methods for it. So I can access this this from another package through this class.

But the problem is I dont want to have a static list, since static variables live in the memory as long as the program executes, and this causing many performance issues. And if there are multiple requests, then this list is with all the request, I want it with specific request only.

Can anyone suggest an alternative of static variable??

Skeleton:

package pa;

class ServAdp { //called multiple times, whenever service is called

static arraylist;

XYZ.set(arraylist);

}

class XYZ{

get(arr);

set(arr);

}


package pp;

class WantTheArraylistHere{

XYZ.get(arraylist);

}

Don't want to make the list static! Any alternative?

网友答案:

You can make this class singleton. Then there will be only one instance and you do not need to make this as static as well.

分享给朋友:
您可能感兴趣的文章:
随机阅读: