开发者

What about object instances reuse?

开发者 https://www.devze.com 2023-04-10 09:52 出处:网络
I am wondering if there is any convention in Java (through Javadoc or implicit convention) to indicate that a given same class instance can be reuse in several contexts ?

I am wondering if there is any convention in Java (through Javadoc or implicit convention) to indicate that a given same class instance can be reuse in several contexts ?

I am asking that because in the SWT layout context I don't know if I am allowed to reuse the same GridLayout object for several Composite objects. I guess the answer is Yes after checking the source code (I can't see any state fields) but the Javadoc doesn开发者_开发技巧't explicitly state it. Maybe the implicit convention is that "sharing" is allowed if not explicitly forbidden ?


Maybe the implicit convention is that "sharing" is allowed if not explicitly forbidden ?

There is no such convention.

Instead, you should take the prudent approach; i.e. you should only reuse instances if the Javadocs explicitly states that you can do this safely.

Reading the code and observing that it is safe to share with the current implementation is no guarantee that it will be safe to share in future releases, or in earlier releases. It is a good idea to only rely on behavior that is documented.

0

精彩评论

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

关注公众号