Java Singleton + inner class missunderstand

I have been programming Java for the past two months, but I am an experienced programmer in python and C. I know that I am making mistakes because of this.

I come to this question to clear the warnings of my project in android studio.

I use the Singleton class with inner classes to keep all my configuration settings in one place and allow all other classes to access it with the need to pass the configuration.

Here is the base code of my Singleton

public class syscfg {

    public List<CommData> Commlist;
    public static CommConfigIP4 MyCommConfig;// = new CommConfig();

    private static syscfg instance = null;
    private static boolean ConfigStat = false;

    /** JAVA singleton control methods**/
    protected syscfg(){
        // pues eso

        if(ConfigStat == false){
            Log.i("SD_app_log", "SYSCFG: Module Initialization");
            ConfigStat = true;
            MyCommConfig = new CommConfigIP4();
            init_config();
        }else{
            Log.i("SD_app_log", "SYSCFG:  Module Loaded");
        }
    }

    public static syscfg getInstance(){
        if(instance == null){
            instance = new syscfg();
        }
        return instance;
    }

    public class CommConfigIP4{
        public int discoveryPort = 30303;
        public  byte[] MyMAC;
        public  String MyType = "";
        public  String MyIP;

        public  byte[] getIPbytearray(){
//            byte[] IPout= new byte[4];
            try{
                byte[] IPout = (InetAddress.getByName(MyIP)).getAddress();
                return IPout;
            }catch (Exception e){
                return null;
            }

        }

In my java file / class post I have:

public class Communications {

    private syscfg CFid ;
    ...
    public Communications(Context ctx){
        ...
        CFid = syscfg.getInstance();
        init_comms(); //init_comms calls whoami
    }

    private void whoami (){
        ...
        CFid.MyCommConfig.MyType = netint.getName();
        ...
    }
}

, (, ) syscfg, Android , . , . nullpointexception

CFid.MyCommConfig.MyType = netint.getName();

, CFid.MyCommConfig = null

singleton, syscfg , .

- , CommConfigIP4 static, , :

syscfg.MyCommConfig.MyType = netint.getName();

.

? ?

,

+4
3

whoami() :

CFid.MyCommConfig.MyType = netint.getName();

... , "MyCommConfig" "syscfg", "CFid" . , "syscfg" ( ) "MyCommConfig" ( ).

"MyCommConfig" , "syscfg.MyCommConfig", , , .

, Java- , Java-.

0

:

public static class CommConfigIP4

, .

0

, . , :

public class Syscfg {

    public Config getConfig() {
        return c;
    }

    // Obtain the same instance always
    public static Syscfg getInstance() {
        return s == null ? new Syscfg() : s;
    }

    // Important for singleton
    private Syscfg() {
        c = new Config();
    }

    private static Syscfg s;
    private Config c;

    class Config {

        public String[] getConfigs() {
            return configs;
        }

        private String[] configs = {"10.10.10.10", "userName", "userPass"}; 
    }

}

, , , Test class,

public class Test {

    public static void main(String[] args) {
        System.out.println(Arrays.toString(Syscfg.getInstance().getConfig().getConfigs()));
    }

}

: [10.10.10.10, userName, userPass]

0

Source: https://habr.com/ru/post/1608707/


All Articles