Constructor chain without this ()

I understand that the chain of constructors goes from the smallest constructor to the largest. for instance

public MyChaining(){ System.out.println("In default constructor..."); } public MyChaining(int i){ this(); System.out.println("In single parameter constructor..."); } public MyChaining(int i,int j){ this(j); System.out.println("In double parameter constructor..."); } 

Also, as I understand it, the call to this() and super() should be on the first line. But is it possible (and if so, is it effective) to circumvent this limit and the circuit designers are different?

For example, I have two constructors that have some code.

  public Location(String _Name) throws IOException, JSONException { //Three lines of unique code (must be executed before the shared code) //Shared code } public Location(JSONObject json) { //Shared code } 

Is it possible for the first constructor to call the second?

+5
source share
4 answers

Sure. If you have a static function

JSONObject foo(String)

then you can write

 public Location(String _Name) throws IOException, JSONException { this(foo(_Name)); } 
+4
source

The only way to run the generic code without calling this is that both constructors call some initialization method that will contain the generic code.

 public Location(String _Name) throws IOException, JSONException { //Three lines of unique code (must be executed before the shared code) init (); } public Location(JSONObject json) { init (); // perhaps the JSONObject should be passed to that method, otherwise // that parameter is useless. On the other hand, I don't see a // JSONObject in the other constructor } private void init () { // shared code } 
+3
source

Somehow another answer: do not do this.

Constructors are like any other members of your class - they should make sense "in general." The presence of designers who offer completely “different” or “independent” “interfaces” to the user looks like a smell of code to me.

You see; if you can draw a line through your class; and you will find that some elements go on the left side, and many others go on the right side of this line; then this indicates that perhaps you should divide your class along this “line” and create two classes instead.

If you really want to create the same object from different contexts; then try to define "denominator unit X"; and provide a constructor specifically for that X. Then use factory methods that can use this common constructor in different ways. This means that you either provide static methods in the same class, e.g.

 static Location generateLocationFromFoo(Foo foo) { ... } static Location generateLocationFromBar(Bar bar) { ... } 

Or take one more step and create a separate factory class that is used to generate the actual location objects for you.

+1
source

I find the chain from the smallest constructor to the largest template, quite unreadable, as the actual logic of the constructor is propagated through many different methods.

I consider it the most readable:

  • Have one "main" constructor that fully handles instance initialization
  • Ask all the other designers to call it.

I also do not like the long chain (it’s hard to get to the actual code if you are drilling from a simple constructor), so I prefer to write all the other constructors to directly call the “main” one, if that does not mean duplicate code.

You can always get around the " this() should be the first statement" constraint by invoking the static method unless you try to do what the rule actually tries to prevent (for example, using a semi-initialized object). Note that the function you call must be static exactly so that you do not use a semi-initialized object.

+1
source

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


All Articles