Valentin Emil Cudelcu Valentin Emil Cudelcu - 5 months ago 9
Java Question

Java - use of super() in the given example

I've just started to learn the Spring framework and I found some tutorial at javatpoint.com.

I've got this code (nothing special, only prints some questions and answers):

private int id;
private String name;
private Map<Answer,User> answers;

public Question(){}
public Question(int id, String name, Map<Answer,User> answers){
super();
this.id = id;
this.name = name;
this.answers = answers;
}


My question is : Why is he using that empty constructor and the keyword super() ? The app works without them and I don't get what are they good for in this code.

P.S. : There is no super class or something like that.

Answer

A no-arg constructor (e.g., Question()) can be useful in several cases - if you're using a class as a bean and wish to lazily initialize its members, serializing it over GWT, etc.. Without any context provided in the example, this is indeed redundant.

The call to super() is completely redundant, and would be performed implicitly if no [other] call to super is specified.

Comments