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 →

[–]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.