开发者

Regarding initialization in the main method

So lets say there's a class SimpleGame with main method(slick2d btw):

public static void main(String[] args) throws SlickException {
    AppGameContainer app = new AppGameContainer(new Simple开发者_运维技巧Game());

Let's say this class has many initialization going on in the class fields declaration, wouldn't it do many unnecessary classes and fields?

I saw a lot of code use this and I never understood that.


wouldn't it do many unnecessary classes and fields?

No, since the main method is static, no fields are initialized just by starting the application.

You could imagine doing something like new AppGameContainer(this), but as you probably know, the this reference is not available in static methods.

The reason to just do something like new AppGameContainer(new SimpleGame()) in the main method is to "escape" the static context, and to get hold of a proper instance with fields etc. (All fields would otherwise have to be static too, which is considered bad practice.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜