Where to put super() or this() in my classes

2019-04-28 05:40发布

I have a Servlet class I made to handle functions I don't want to repeat on every Servlet I have. I'm still working on it (i.e. it still only loads index.jsp and not other files).

public class MyServlet extends HttpServlet {
    public MyServlet () {
        super();
    }

    public void loadView (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");

        response.setContentType("text/html;charset=UTF-8");

        System.out.println("MyServlet::LoadView() success");

        dispatcher.forward(request, response);
    }
}

My Servlet is as follows

@WebServlet(name = "EditServlet", urlPatterns = {"/content/edit"})
public class EditServlet extends Library.MyServlet {
    public EditServlet () {
        super();
    }

    public void doGet (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        System.out.println("EditServlet loaded");
    }
}

I am however, unable to compile my code:

SEVERE: Exception while deploying the app [ContentManagement] : (class: contentmanagement/content/EditServlet, method: <init> signature: ()V) Constructor must call super() or this()

update

Ok, removing void on my constructors and calling super(); got the must call first portion to go away, but it's still saying I have to call super despite that it's already being called.

update

I don't understand the responses below.... they keep saying to put super() within the constructor, when my examples already show that being done, AND it is the first line of code. Any other advice would be appreciated.

Any thoughts?

This is now a non-issue. I do not know what resolved this issue, but with multiple changes and rebuilding my app from the ground up I am no longer experiencing this issue

5条回答
萌系小妹纸
2楼-- · 2019-04-28 05:50

Assuming Library.MyServlet has a constructor that takes no arguments, this should work:

public class EditServlet extends Library.MyServlet {
    public EditServlet () {
        super();
        // Rest of initialization code...
    }
    //...
}
查看更多
该账号已被封号
3楼-- · 2019-04-28 06:00

Your problem was:

method: <init> signature: ()V) Constructor must call super() or this()

It seems like VerifyError symptom. Cleaning and rebuilding the project should resolve the problem, see also:


Furthermore, you can remove your constructors with a super() single-line. The JVM does it for you with a default (implicitly hidden) constructor. So, clear these unnecessary lines:

public MyServlet () {
    super();
}

and

public EditServlet () {
    super();
}

You would have to explicitly call super() if you needed to write additional business lines in the constructor.

查看更多
劫难
4楼-- · 2019-04-28 06:01

Just clean and build... It's a build issue, your class is perfectly normal now. Sometimes when you correct the code the IDE just recompile parts of your code and not the entire code. A clean and build should do the trick

查看更多
放我归山
5楼-- · 2019-04-28 06:01

If you use this() or super(), it must be the first line of the constructor.

However, if you need to do some work before calling this() or super(), you can use this pattern:

public MyClass () {
    this(someStaticMethod());
}

or

public MyClass () {
    super(someStaticMethod());
}

And craft other private constructors to suit.

The methods you call can't be instance methods, so it doesn't work for all cases, but it may help you.

查看更多
相关推荐>>
6楼-- · 2019-04-28 06:09

And how does the constructor of Library.MyServlet look?. As a note, if the super keyword is to be used in a constructor, it has to be on the first line. Same thing for this.

Have you tried deleting the empty constructor? Apparently, you're not using it.

查看更多
登录 后发表回答