5

Look at following class (please note, that it is not a singleton):

public MyClass() {

    @Inject private A a;
    @Inject private B b;

}

What object will be created first a or b?

Is there a possibility of determine the order of creating objects?

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
jtomaszk
  • 9,223
  • 2
  • 28
  • 40
  • 1
    Unfortunately, I can't answer your question, however, I can advice you to take a hard look at your classes and try to figure out how not to depend on the order of object instantiation. – Buhb Jan 03 '14 at 14:44

2 Answers2

3

I don't think so and I don't see a reason why it should matter (I am also afraid that order of object instantiation can change from deployment to deployment). You are guaranteed to have all objects injected in @PostConstruct and that's the most important thing (as far as I am concerned).

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
1

Actually, you can very much control order. First, assume that they have normal scopes (e.g. @RequestScoped). Second, make B have a reference to A. You'll see that A gets instantiated first, then B. Note that you'll need to track via your @PostConstruct method.

John Ament
  • 11,595
  • 1
  • 36
  • 45