How to use global class variables in android

后端 未结 4 921
粉色の甜心
粉色の甜心 2021-01-26 09:12

Hi I am doing one app here. I\'m using global class varibles. It\'s working well, but if I\'m using more globalclass variables I\'m getting memory exceptions some times.

4条回答
  •  北海茫月
    2021-01-26 09:39

    You can use like this

    public class GlobalVar {
    
        public int getMyVar() {
            return myVar;
        }
    
        public void setMyVar(int myVar) {
            this.myVar = myVar;
        }
    
        private int myVar = 0;
        private static GlobalVar instance;
    
        static {
            instance = new GlobalVar();
        }
    
        private GlobalVar() {
        }
    
        public static GlobalVar getInstance() {
            return GlobalVar.instance;
        }
    
    }
    

    then you can call like

    GlobalVar.getInstance().setMyVar(int);
    

提交回复
热议问题