In Java, why have a code block with no keywords, just curly brackets

前端 未结 4 1050
感动是毒
感动是毒 2020-12-11 14:49

I\'m re-factoring some inherited code, but was stumped by the design decision and can\'t figure out the proper terms to google this. My predecessor would use blocks like thi

4条回答
  •  眼角桃花
    2020-12-11 15:34

    Your predecessor was still learning.

    That's the best explanation you're likely to get. Perhaps at one point in time there was a need to have the code split up like this. It's hard to say. The code should certainly be written like this instead:

    public class ChildClass extends ParentClass {
        public ChildClass() {
            inheritedVar = "someVal";
        }
        // rest of code
    }
    

    As for the initializer block, its purpose has been given by the other answers here. I threw my answer in as an attempt to answer the "why", which you requested. Unfortunately, for the real answer, you would have to ask your predecessor.

提交回复
热议问题