What is an initialization block?

后端 未结 10 1884
青春惊慌失措
青春惊慌失措 2020-11-22 03:42

We can put code in a constructor or a method or an initialization block. What is the use of initialization block? Is it necessary that every java program must have it?

10条回答
  •  被撕碎了的回忆
    2020-11-22 03:49

    The question is not entirely clear, but here's a brief description of ways you can initialise data in an object. Let's suppose you have a class A that holds a list of objects.

    1) Put initial values in the field declaration:

    class A {
        private List data = new ArrayList();
    }
    
    
    

    2) Assign initial values in the constructor:

    class A {
        private List data;
        public A() {
            data = new ArrayList();
        }
    }
    
    
    

    These both assume that you do not want to pass "data" as a constructor argument.

    Things get a little tricky if you mix overloaded constructors with internal data like above. Consider:

    class B {
        private List data;
        private String name;
        private String userFriendlyName;
    
        public B() {
            data = new ArrayList();
            name = "Default name";
            userFriendlyName = "Default user friendly name";
        }
    
        public B(String name) {
            data = new ArrayList();
            this.name = name;
            userFriendlyName = name;
        }
    
        public B(String name, String userFriendlyName) {
            data = new ArrayList();
            this.name = name;
            this.userFriendlyName = userFriendlyName;
        }
    }
    
    
    

    Notice that there is a lot of repeated code. You can fix this by making constructors call each other, or you can have a private initialisation method that each constructor calls:

    class B {
        private List data;
        private String name;
        private String userFriendlyName;
    
        public B() {
            this("Default name", "Default user friendly name");
        }
    
        public B(String name) {
            this(name, name);
        }
    
        public B(String name, String userFriendlyName) {
            data = new ArrayList();
            this.name = name;
            this.userFriendlyName = userFriendlyName;
        }
    }
    
    
    

    or

    class B {
        private List data;
        private String name;
        private String userFriendlyName;
    
        public B() {
            init("Default name", "Default user friendly name");
        }
    
        public B(String name) {
            init(name, name);
        }
    
        public B(String name, String userFriendlyName) {
            init(name, userFriendlyName);
        }
    
        private void init(String _name, String _userFriendlyName) {
            data = new ArrayList();
            this.name = name;
            this.userFriendlyName = userFriendlyName;
        }
    }
    
    
    

    The two are (more or less) equivalent.

    I hope that gives you some hints on how to initialise data in your objects. I won't talk about static initialisation blocks as that's probably a bit advanced at the moment.

    EDIT: I've interpreted your question as "how do I initialise my instance variables", not "how do initialiser blocks work" as initialiser blocks are a relatively advanced concept, and from the tone of the question it seems you're asking about the simpler concept. I could be wrong.

    提交回复
    热议问题