What type of java constructors? Constructor chain?

This is from spring amqp samples on github at https://github.com/SpringSource/spring-amqp-samples.git what type of java constructors? are they a short hand for getters and setters?

public class Quote { public Quote() { this(null, null); } public Quote(Stock stock, String price) { this(stock, price, new Date().getTime()); } 

as the opposite of that

 public class Bicycle { public Bicycle(int startCadence, int startSpeed, int startGear) { gear = startGear; cadence = startCadence; speed = startSpeed; } 
+1
source share
3 answers

These constructors are overloaded to call another constructor using this(...) . The first no-arg constructor invokes the second with zero arguments. The second calls the third constructor (not shown), which must take the values Stock , String and long . This template, called the constructor chain, is often used to provide several ways to create an object without duplicating code. A constructor with fewer arguments fills the missing arguments with default values, such as new Date().getTime() , or simply passes null s.

Note that there must be at least one constructor that does not call this(...) , and instead provides a super(...) call, followed by a constructor implementation. If neither this(...) nor super(...) specified on the first line of the constructor, the no-arg call to super() implied.

Therefore, assuming there is no constructor chain in the Quote class, the third constructor probably looks like this:

 public Quote(Stock stock, String price, long timeInMillis) { //implied call to super() - the default constructor of the Object class //constructor implementation this.stock = stock; this.price = price; this.timeInMillis = timeInMillis; } 

Also note that this(...) calls may be followed by implementation, although this deviates from the chaining pattern:

 public Quote(Stock stock, String price) { this(stock, price, new Date().getTime()); anotherField = extraCalculation(stock); } 
+5
source

This is what we call the telescopic pattern. But the method you used in the Quote class is not useful. For example, think that in your class you have one required property and two additional properties. In this case, you need to provide a constructor with this required property, and then inside this constructor you need to call other constructors with default values ​​for optional parameters.

 // Telescoping constructor pattern - does not scale well! public class NutritionFacts { private final int servingSize; // (mL) required private final int servings; // (per container) required private final int calories; // optional private final int fat; // (g) optional private final int sodium; // (mg) optional private final int carbohydrate; // (g) optional public NutritionFacts(int servingSize, int servings) { this(servingSize, servings, 0); } public NutritionFacts(int servingSize, int servings, int calories) { this(servingSize, servings, calories, 0); } public NutritionFacts(int servingSize, int servings, int calories, int fat) { this(servingSize, servings, calories, fat, 0); } 

I am extracting this Java code from efficient java edition 2.

+1
source

It invokes a constructor that takes an integer argument.

This is what I found on the Java Tutorials web page:

You can access any member of the current object from an instance method or constructor using this.

In this case, it calls something from the ArrayList class. This was also in the Java Tutorials section:

Inside the constructor, you can also use this keyword to call another constructor in the same class. This is called an explicit constructor call.

So, your vision in this case is Invicit Constructor Invocation

Source: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html

0
source

Source: https://habr.com/ru/post/916124/


All Articles