Skip to content

What’s the difference between the two expressions?

public @PostConstruct void init()

and

@PostConstruct
public void init()

What’s the difference between them in java? It seems that the generated bytecode is identical.

Answer

It seems that the generated bytecode is identical.

Then you got your answer. Semantically, the two constructs will result in the exact same behavior at runtime.

So the only thing left: what does it do “to the source code”? And there it is pure style. And to style questions, there are two simple rules to follow:

  • Pick the style that works best for you/your team/your product, so do what will be the least surprising for the other people reading this code
  • But then, when there isn’t much precedence in your group, then you follow the informal conventions of Java.

Regarding that 2nd point, I have never seen a method annotation written down like in your option 1. “The whole world” will write the annotations before the actual signature line.