📜  如何在java代码示例中将类对象初始化为null

📅  最后修改于: 2022-03-11 14:52:21.408000             🧑  作者: Mango

代码示例1
protected void doPost(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
    // Note that unless except in certain cases (IoC)
    // this scenario is quite easy to avoid
    Object_DTO object_DTO = null;

    if(request.getParameter("parameter").equals("hello")) {
        object_DTO = new Object_DTO(); // here null object initialized to a new object
        object_DTO.setAttr("attr");
        ...
    } else if (request.getParameter("parameter").equals("goodbye")) {
        object_DTO = new Object_DTO();
    }

    if (object_DTO == null) {
        // Bad response
    }
}