Java constructors for side effects only

I have a Java instance class, "ResultMaker", which exists only for side effects (sending email), and the object is never used afterwards. (Of course, this can be rewritten so that ResultMaker is a static class, but when I add a “class” to the class, adding methods and making multiple instances, I think that in the end I want to have a reference to the object, so my rationale for declaring ResultMaker as an instance class at this point is that the code is not finished yet.)

To clear the Java warning that it’s rnot being used (see the code for a short example), I removed the variable assignment r(see the code), so I only had to new ResultMaker(ans). A side effect stopped with code changes, so I think that creating an instance of ResultMaker is no longer happening. Does this deserve a compiler warning (that there will be no instance) or is it a well-known aspect of Java?

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException 
    {
        Thing tt = new Thing();
        String answer = tt.DoSomething();

        ResultMaker r = new ResultMaker(answer);

        resp.getWriter().println(answer);
    }
+3
source share
2 answers

, , , . , . , , , - , , - - , - .

, , - , , , , , , . , .

EDIT: , , , :

catch (UnsupportedEncodingException e)
{
}
catch (AddressException e) 
{
} 
catch (MessagingException e) 
{
}

, , .

. , .

+4

, ResultMaker (MailSender?), - r.sendMail(content). Spring Guice, . ServletContext ResultMaker, .

+1

All Articles