How do I persist data without global variables?

后端 未结 5 1022
傲寒
傲寒 2020-12-18 00:06

I\'m used to scripting languages. PHP, Javascript etc. and I\'ve written a few relatively simple Java and C# apps. This is a question I\'ve repeatedly needed an answer for,

5条回答
  •  攒了一身酷
    2020-12-18 00:22

    This is not a good practice, but if you really need it, there is a number of ways:

    1. Web apps: You can put your variable in some kind of context, like the session or the application scope.
    2. Desktop apps: You can create an object and store it as a property of a class that always have an object active.
    3. Any kind of app: use a public static property. It is visible to everyone.

提交回复
热议问题