It seems to me constructors can share the same code, for example in:
public class Foo {
private int foo;
{foo = 5;}
public Foo(){}
public Foo(int v){this.foo = v;}
public int getFoo(){return foo;}
}
The code "foo=5;" is called for both constructors.
It seems to me you can not, but I want to make sure. It not possible to create such common code that use parameters ?
eg, something like:
public class Foo {
private int foo;
(int m){foo = m*5;}
public Foo(int m){}
public Foo(int v,int m){this.foo = v;}
public int getFoo(){return foo;}
}
To my understanding, the only way is to create a private void init(int m) to be called by all constructors ?
ps: I call {foo = 5;} "common code", but I guess this feature has another official name ?
EDITS (1):
The term I was looking for is initializer block
This question is not the same as asking if a constructor can also call another constructor. Because when using an initializer block, the code is called AUTOMATICALLY, ie. without risk to call a constructor and forget to call it
My comment about using "void init" was not good, indeed in this case calling another constructor is better.
In short, my question: can an initializer block takes parameters ? Which would be kind of the same as forcing some parameters on all constructors to be implemented.
EDITS (2):
I now wonder if the only way to achieve what I am asking is to use inheritance to force the use of a specific constructor.