@BeforeClass vs static{}

前端 未结 4 679
难免孤独
难免孤独 2020-12-30 00:18

I am writing some test cases using JUnit. I need to initialize some static variables which will be used for all the test cases in that class.

For this I can use eith

4条回答
  •  悲哀的现实
    2020-12-30 01:07

    If it's both static and final then you only have one choice: static initializer.

    The compiler will prevent you from writing to a final field from inside a method, static or not.

    EDIT: OK, you've removed the word 'final' from your question. In that case, it makes little difference. The static initializer will run once; so will the @BeforeClass method. Just pick whichever one you think is more readable.

提交回复
热议问题