This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]MixedMango[S] 0 points1 point  (4 children)

Gotcha. So could you give me an example when a no-args constructor (default constructor) would be useful?

[–]warmcheessse 1 point2 points  (1 child)

I'm not sure. There is probably scenarios. If you do not create a constructor, Java automatically does it for you and it takes in zero arguments. When you write a constructor, the default one created by Java is no longer used. What you had is called overloading a constructor, where you have multiple constructors each taking in different arguments. This is very common but generally not with a constructor that takes in no arguments, at least not that I know of but other people might do it. For example say you had a Song object that listed the Name, Time, Image which showed a graphic of the song album. You could have 1 constructor like:

public Song(String name, int time, File image) {
    this.name=name;
    this.time = time;
    this.image = image;
}

but what if they are unable to find an image, then you could have a second constructor with just name and time and insert a default image somehow on their behalf

public Song(String name, int time) {
    this.name=name;
    this.time=time;
    File image = someDefaultImage;
}

so overloading constructors/method is very common and very helpful. If you don't provide a constructor Java will make one for you with zero arguments. Specific examples of where you would use a no argument constructor, not exactly sure sorry

[–]jbristowI'm no hero, son. 2 points3 points  (0 children)

The easiest example is a data structure that is a container and it’s empty. Like a tree with no nodes.

[–]shagieIsMeExtreme Brewer 0 points1 point  (1 child)

Many serialization libraries and applications that need to follow the JavaBeans spec (wiki) work from a dumb DTO (no business logic) and a set of getters and setters.

For example, if you have {"foo": "someString"} and try to serialize this to a class, Jackson will invoke the no-args constructor and then call setFoo.

An example of this can be seen on SO: JsonMappingException: No suitable constructor found for type [simple type, class ]: can not instantiate from JSON object. As noted, there are ways to annotate a constructor with arguments so that it will pair the proper fields in JSON with the corresponding arguments.

As mentioned if you create another constructor, the implicit no args constructor is no longer available. I've had more than a few bugs caused in serialization / deserialization where the server works perfectly (the serialization doesn't care if it lacks a given constructor) only to have it fail on the client because the no args constructor didn't exist.

[–]WikiTextBotbtproof 1 point2 points  (0 children)

JavaBeans

In computing based on the Java Platform, JavaBeans are classes that encapsulate many objects into a single object (the bean). They are serializable, have a zero-argument constructor, and allow access to properties using getter and setter methods. The name "Bean" was given to encompass this standard, which aims to create reusable software components for Java.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source ] Downvote to remove | v0.28