Does javac removes methods that are not referenced in the code?

那年仲夏 提交于 2019-12-10 03:06:09

问题


I have a code base and some methods are never used. Does javac remove the unused methods from the class file?


回答1:


Q: I want to know if I have a code base and some methods are never used. Does javac remove the unused methods from the class file?

A: No. What goes into the class, stays in the class file.

... however ...

The JVM loads only what's needed into memory. RAM isn't "wasted" on unused classes.




回答2:


No, it doesn't. To verify this, you can run

javap -c foo.bar.MyClass

and see all the code there. You can also access it via reflection (assuming you're running with appropriate permissions).




回答3:


No it doesn't and it can't. Think about what would happen if the compiler did that when you compile a library. All methods that the library wants to export for users, but doesn't use itself would be removed. And there is no way in Java to distinguish between something that is a library and your code.



来源:https://stackoverflow.com/questions/9610163/does-javac-removes-methods-that-are-not-referenced-in-the-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!