0

We can do :

class A {
     // some code
}

class B {
    A obj = new A(); // ???
}

Query :

  • My understanding is that the instance obj (of class A) is not static therefore not available in Class memory of B
  • In that case, will it be initialized for every instance of B?
  • In the case that obj actually stays in Class memory of B (i.e., static), then when will the initialization of instance obj happen?
  • Either way, is this a good practice ?
  • Then why do we have constructors if we can do this?

Feel free to correct me.

Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197

3 Answers3

6

Fields initialized like this are common practice.

You can think of them as being part of the constructor, similar to instance initializer blocks.

We still have constructors, because you want to take parameters and have complex logic sometimes.

The pattern is especially convenient if you have more than one constructor (the fields will be initialized for every code path).

Thilo
  • 257,207
  • 101
  • 511
  • 656
0

A is not static therefore not available in Class memory.

Right.class A will be instantiated whenever B is instantiated

will it be preinitialized for every object of B ?

Yes.Whenever you are creating object of B, A object is also created

either way is it a good practice ?

Practically it is the same as instantiating through a constructor. So no problems of being a good or bad practice

Then why do we have constructors if we can do this ?

If you have multiple constructors, then you don't have to instantiate A in every constructor.

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56
  • "Compiler knows that `B` requires `A` so `A` will be instantiated when `B` is instantiated." is a bit misleading. `A` will be instantiated when `B` is instantiated doesn't have anything to do with `compiler knows that `B` requires `A` in the truest sense. – justhalf Oct 01 '13 at 03:40
  • A will get compile irrespective of whether B uses it or not. – Arham Oct 01 '13 at 03:42
0

A is not static therefore not available in Class memory.

Correct. You have to have an instance of B to get at the field A obj.

will it be preinitialized for every object of B ?

Yes. A separate new A will be created for every B created.

If this has to stay in obj memory then when will this initialization happen ?

N/A

Either way is it a good practice ?

Yes. It's called object composition and composing objects from other objects is one of the two main means of decomposing a problem using Object-Oriented design. The other one is inheritance.

Then why do we have constructors if we can do this ?

This is just syntactic sugar. All the below are equivalent.

class B {
  A obj = new A();  // field initializer
}

class B {
  A obj;
  B() {
    A = new A();  // initialized in constructor
  }
}

class B {
  A obj;
  { obj = new A(); }  // instance initializer
}

As long as nothing reads obj during initialization, there's no observable difference between doing initialization in a field, constructor, or explicit initializer.

Sometimes it's just more convenient to assign a value where the field is declared.

Mike Samuel
  • 118,113
  • 30
  • 216
  • 245