5

I came across the following:

class Foo {

  static {
    // setup for logging and configuration
  }

  public static void setup() {
    // do nothing
  }

}

Is this idiomatic Java code? Are there specific reasons this should be avoided?

This was found in a test suite.

My initial concern was that initialization could theoretically happen without calling #setup().

user3268232
  • 227
  • 1
  • 8
  • Well, for one, it prevents subclasses of `Foo` from having their own implementation of `setup()`... Would be interested to see what gurus say. – mazaneicha Mar 08 '15 at 17:37
  • 1
    @mazaneicha There's no problem with a `Bar extends Foo { public static void setup(){...} }` – laune Mar 08 '15 at 17:39
  • @mazaneicha no it doesn't – Sean Patrick Floyd Mar 08 '15 at 17:46
  • I meant "prevents subclasses from overriding `setup()`". Yes hiding it will not be a problem. – mazaneicha Mar 08 '15 at 17:59
  • if `setup` method does nothing then it makes no sense of it's existence – Neeraj Jain Mar 08 '15 at 18:04
  • 1
    @mazaneicha There is no such thing as "overriding a static method" in Java. This applies to "true" methods, but a static method isn't associated with an instance of a class, so the concept doesn't apply here. And you cannot "hide it" (if I read you correctly). – laune Mar 08 '15 at 18:11

3 Answers3

0

Is this idiomatic Java code?

No.

Are there specific reasons this should be avoided?

Yes: it's confusing, as evidenced by the comments to this question.

My initial concern was that initialization could theoretically happen without calling #setup().

I would guess the author's concern was that initialization might not happen without calling setup(). The setup() method guarantees that the static initializer has executed its "setup for logging and configuration". Presumably, there are side effects to this configuration which the author seeks to ensure by calling the empty method.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
0

Was the code you posted precisely what you'd come across? In JUnit, it is common practice to use a static method annotated with @BeforeClass to configure the test class, and an instance method annotated with @Before to configure each unit test.

(Here are examples of their use: http://examples.javacodegeeks.com/core-java/junit/junit-before-and-beforeclass-example/)

Otherwise, it does seem odd...

Keith
  • 3,079
  • 2
  • 17
  • 26
  • See the OP's [comment](http://stackoverflow.com/questions/28929588/is-using-an-empty-static-method-in-a-class-that-has-a-static-initializer-accepta/32107117?noredirect=1#comment46115409_28929792) indicating the purpose of the `setup()` method is not part of JUnit. – jaco0646 Aug 20 '15 at 00:47
0

It's definately not idiomatic as you are restricting those static methods and blocks from inheriting to sub-classes. And yeah,for the Initialization purpose static methods won't need to be called as they are initialized at time of class loading into JVM. And while executing, static blocks are first to execute from top-bottom.

Ayush
  • 87
  • 1
  • 8