Java singleton internal class error understanding

I have been programming java for the past two months, but I have experienced programmers in Python and C I know I made a mistake because of it

I came to this issue to clear the project warning me in Android studio

I use the singleton class with an inner class to save all configuration parameters in one place so that all other classes can access it without passing the configuration

This is the basic 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 communication java file / class, 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();
        ...
    }
}

Therefore, when I first own all the elements (variables, classes and methods) in syscfg, the static Android studio will display a warning that static members are accessed through instance references After some research and documentation, I found that static variables and methods are not recommended. I tried to eliminate them all But then I got a nullpointexception error

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

Using the debugger, I found cfid MyCommConfig = null

I use singleton to avoid using static on syscfg class and access by instantiation instead of class name

Now my singleton code is like the commconfigip4 static code published here. I warn again and suggest that I use:

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

Instead of using instances to access the configuration

What happened here? What did I miss?

Thank you, Guillermo

Solution

In your whoamI () method, you made this reference:

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

... however, "mycomconfig" is a static attribute of class "syscfg", and the variable "cfid" refers to an instance of this class In other words, all instances of "syscfg" (and @r)u 419)u 2488@ itself) refer to the same copy of the "mycomconfig" variable (which means static)

Therefore, it is not so confusing to refer to the "mycomconfig" variable by saying "syscfg. Mycomconfig", because it indicates that you are referring to static variables rather than instance 1

By the way, you should consider following standard Java code conventions to capitalize class names and variables, as this will make your code more readable to other Java programmers

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>