Any way to _not_ call superclass constructor in Java?

后端 未结 13 1363
误落风尘
误落风尘 2020-12-15 04:16

If I have a class:

class A {
   public A() { }
}

and another

class B extends A {
   public B() { }
}

is t

13条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-15 04:29

    Java deserialisation doesn't call the constructor, but it seems that it is based on some internal JVM tricks.

    However, there is a framework that allows you to do that in a portable manner: Objenesis (http://www.theserverside.com/discussions/thread/44297.html)

    I've seen this recently in Spring, when using CGLIB proxies, Spring creates two class instances, but the constructor is called only once: https://stackoverflow.com/a/11583641/2557118

    This behavior is added in Spring 4:

    CGLIB-based proxy classes no longer require a default constructor. Support is provided via the objenesis library which is repackaged inline and distributed as part of the Spring Framework. With this strategy, no constructor at all is being invoked for proxy instances anymore.

提交回复
热议问题