initialize object directly in java

前端 未结 10 1017
逝去的感伤
逝去的感伤 2020-12-08 04:26

Is that possible to initialize object directly as we can do with String class in java:

such as:

String str=\"something...\";

I wan

10条回答
  •  温柔的废话
    2020-12-08 05:07

    The following does what you want, but not in the way that you would expect.

    So in a class calling MyData, you would use

    Public MyData x = new MyData();
    @PostConstruct public void init() {
         x.setName("Fering");
         x.setAge(18);
    }
    

    So once the object is construsted, these commands are run, which allows you to populate the object before anything else runs.

    So with this you do not have to use anonymous subclasses, or create new constructors, you can just take the class and then use its functions, before anything else would.

提交回复
热议问题