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

- 19,665
- 4
- 70
- 110

- 3,868
- 2
- 24
- 35
-
1Not if the methods are `public`, I should hope. – Fred Foo Mar 07 '12 at 22:33
-
@larsmans: Why is it special for `public`? Is it because some other code might be using them? Can you elaborate? – noMAD Mar 07 '12 at 22:35
3 Answers
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.

- 114,292
- 17
- 138
- 190
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).

- 1,421,763
- 867
- 9,128
- 9,194
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.

- 2,277
- 15
- 22