Why might one also use a blank constructor?

前端 未结 10 581
傲寒
傲寒 2020-12-16 04:59

I was reading some Java recently and came across something (an idiom?) new to me: in the program, classes with multiple constructors would also always include a blank constr

10条回答
  •  北海茫月
    2020-12-16 05:13

    The default constructor is'nt a good pratice for the functional view. The default constructor is used if the object have a global visibility into a method: for example, you want log the actual state of a object in a try/catch you can code

    MyObejct myObject=null
    try{...
    }catch(Exception e){
        log.error(myObject);//maybe print null. information?
    }
    

    or do you prefer

    MyObejct myObject=new Object();
    try{...
    }catch(Exception e){
    log.error(myObject);//sure print  myobject.toString, never null. More information
    }
    

    ?

    Anotherway the create a EMPTY object have'nt a lot of logic, but instatiate a NULL object is harmuful in my opinion. You can read this post

提交回复
热议问题