Why can't an inner class use static initializer?

后端 未结 3 1911
陌清茗
陌清茗 2021-01-13 07:19

Quoth JLS #8.1.3:

Inner classes may not declare static initializers (§8.7)......

This is demonstrated as such:<

3条回答
  •  心在旅途
    2021-01-13 07:38

    No Valid Use

    just an opinion I came by with, arguments/debates are appreciated

    Please read the below thread.

    This explains, Why does Java prohibit static fields in inner classes

    IMO the same reason also applied to static initializer. After all, the thing that creating problem is the keyword static.

    Added to the reason explained in above thread, I can give another lame reason.
    The name of the block static initializer give us a hint on when and why to use this block. One does not simply use static initializer block to print hello world [insert the meme here].
    The main reason to use this block is clearly to initialize static variable.

    Now as inner class/ non-static nested class don't allow static variable, what is the point of allowing static initializer?

提交回复
热议问题