4

Is there a anyway to add static code block into java class using javapoet library

static {
        // whatever code is needed for initialization goes here
    }
Damith
  • 1,866
  • 3
  • 27
  • 38
  • Possible duplicate of [Generating static class initializer using javapoet](https://stackoverflow.com/questions/29828721/generating-static-class-initializer-using-javapoet) – Denis Itskovich Dec 04 '18 at 06:28
  • @DenisItskovich I have reversed the vote to close, as the answer here actually shows what to use, while the answer on your question only says that it isn't yet possible and will be introduced in a future version. In other words, this question is more valuable in that sense. – Mark Rotteveel Dec 06 '18 at 15:19

1 Answers1

3

Use TypeSpec.Builder::addStaticBlock. See this test case for an example.

Sormuras
  • 6,622
  • 29
  • 56