4

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):

  1. The term I was looking for is initializer block

  2. 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

  3. My comment about using "void init" was not good, indeed in this case calling another constructor is better.

  4. 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.

Vince
  • 3,979
  • 10
  • 41
  • 69
  • possible duplicate of [Why is this Java code in curly braces ({}) outside of a method?](http://stackoverflow.com/questions/5865069/why-is-this-java-code-in-curly-braces-outside-of-a-method) –  Nov 09 '13 at 03:14
  • {}/common code is initializer – gjman2 Nov 09 '13 at 03:16
  • @Vince can you reword that? You can call other constructors from inside a constructor using `this(params p)` or `super()` (for the parent class). – 0x6C38 Nov 09 '13 at 03:18
  • possible duplicate of [How do I call one constructor from another in Java?](http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java) – 0x6C38 Nov 09 '13 at 03:18
  • please see my comment in the answer below. My question is slightly different. – Vince Nov 09 '13 at 03:48

2 Answers2

6

You can call your constructors from other constructors by calling this(), and matching the parameter list.

Say i have:

Foo(int a, int b) {
  // some code..
}

but i want to also call:

Foo(int a, int b, int c) {
  // some other code
}

I would say this:

Foo(int a, int b) {
  this(a, b, 0);
  // Whatever other code you want in this constructor.
}
yamafontes
  • 5,552
  • 1
  • 18
  • 18
  • Hi, my question is slightly different, as in my question the "common code" is automatically called, meaning no risk of creating a new constructor and forgeting the call to this(...). This being said, this answers perfectly my last comment (the only way is to use init() ...). – Vince Nov 09 '13 at 03:46
0

You need to understand the way how Java creates objects!Assume the you have

class A{

private String b = "b";
private static String a = "a";

{
   b = "b2";
}

static{
 a = "a2";  
}

public A()
{
 b = "b3";
}


} 

So the idea is that when you create an object

new A();

The first

private static String a = "a";

After

static{
 a = "a2";  
}

After

private String b = "b";

After

{
   b = "b2";
}

And only after

public A()
{
 b = "b3";
}

But be careful the priorities of static variables and static blocks are the same. And the priorities of normal variables and blocks are again the same. So if you put the next code

static{
 a = "a2";  
}

and after

private static String a = "a";

you will have that the code in the block is ignored, because you use the variable before declaration! And the same for normal variables!

Anton
  • 731
  • 4
  • 5
  • By the way, in .Net the priority of fields higher than blocks so you can put block before declaration – Anton Nov 09 '13 at 03:36
  • Last paragraph is not correct. A static or instance initialization block that uses a forward reference to a variable declaration is not an error. Try it. – user207421 Nov 09 '13 at 05:05
  • Yes but neither in a static nor in a normal block you will see assignment... so it looks like the variables are just ignored! I meat not a compile error I meant that it doesn't work.But it works in c#. So even if a block comes first the assignment happends – Anton Nov 09 '13 at 05:28
  • I cannot understand any of that, but your edit is still wrong. The block is not ignored, as you can easily verify for yourself. The initialised declaration causes *another* assignment. They are both executed, in the order they appear. – user207421 Nov 11 '13 at 21:35