Java Constructor Chaining - 034

preview_player
Показать описание
In this tutorial, we are looking at Java constructor chaining. Constructor chaining is chaining our constructors together, and calling each one in order before returning a created instance.





Concepts: Java, classes, java constructor chaining, constructors

Social Links: Don't hesitate to contact me if you have any further questions.

Related Videos:

Hide your class details with Java Access Modifiers - 027

Free Java Course Online

Media credits: All images are owned by DJ Spiess unless listed below:

Brick Background

Music
Рекомендации по теме
Комментарии
Автор

I'm super gay and hate football but it was still an amazing explanation, thanks.

bennyburwell
Автор

Will the code compile if you are not referring to this.hometeam in your second constructor because they have the same name ?

hrban
Автор

I like your explanation. But to my opinion you make a mistake in your example. See 3:24. You have to use the default values twice. And in the all-args constructor you first call the default and set the values to the default and directly pass the arguments. That does not make sense. I think this should be the correct way - default calls one-args which in turn calls all-args constructor

public FootballGame() {
this(2011)
}

public FootballGame(int year) {
this(year, "Arizona", "Denver")
}

public FootballGame(int year, String homeTeam, String awayTeam) {
this.year = year;
this.homeTeam = homeTeam;
this.awayTeam = awayTeam;
}

zedjurk
Автор

Why we use constructor chaining in java

lokeshloki
Автор

what if the constructor doesn't take any argument?

maazahmedpoke
Автор

Very good explanation but the example could have been better. The specific chains chosen make it hard to understand the concept. Why would a constructor that takes a year assume the team names? Something like "Home Team", "Away Team" would make it more clear what is going on, and would prevent the internal monologue of "it is just an example" that detracts from an otherwise great explanation.

insmallportions
Автор

String comment = "I'm more confused about constructor chaining than ever before, thanks for nothing!";

DanielADickey