开发者

Is it possible to call the finalize method to a int or vector?

Is it p开发者_Python百科ossible to call the finalize method to a int or vector in Java?


An int is not an object and does not have methods.

A java.util.Vector has (like any object) a finalize() method, but it is called automatically when the object is garbage collected.

You should never write a finalize() method that is meant to be called explicitly, because that would be a flagrant violation of the principle of least surprise.


I think some are "freaking out" because it has so many incorrect assumptions in the question.

int is a primitive so it doesn't have a finalize method to call.

Vector is a legacy class which was replaced in Java 1.2 (1998) by ArrayList. It has a finalise method which is protected. protected means it should only be called from a sub-class. So you cannot access it easily.

However, I don't suggest you call finalize on Vector or any other class. If you have a finalize method which has functionality you would like to call, create a public method which does the same thing rather than call finalise()

BTW: Even if you did call Vector.finalize() it doesn't do anything. It inherits Object.finalize() which is {}


What is your motivation for this and how does this relate to garbage collection? To make an object collectable, you need to remove all strong references to it, not call finalize.

Please read this important reference on Garbage collection, and in particular the section on Finalized, section A.3.6 The Truth About Garbage Collection


The method finalize() is inherited from java.lang.Object. Java int is a primitive type and these types are not considered objects, therefore it is not possible to invoke finalize on these.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜