Advertisement

[java] final class speed?

Started by November 15, 2000 11:40 AM
2 comments, last by moregames 24 years, 2 months ago
If I create a final class so that it can''t be subclassed, and access methods in that class from 100 other classes all at once is there a slow down since no new (100) instance is created?
Final classes can''t be extended but if you want you can have a million instances of it if you wanted. Maybe I am not understanding your question but it should not slow down your code at all. In fact final methods can be inlined and will speed up code.

I wanrned you! Didn''t I warn you?! That colored chalk was forged by Lucifer himself!
"... we should have such an empire for liberty as she has never surveyed since the creation ..."Thomas Jefferson
Advertisement
Final classes makes all methods in a class final. This avoids an extra level of indirection (through the virtual table internal in the run time system) and is faster, but not much.

So final classes are faster than non final classes when you call methods in them, but it is bad style doing so and should be avoided if possible. At least don''t do it until your application is completely done.

If you want speed and not ease of use (which you loose by applying optimization tricks like this) then you might as well use C++ in the first place.

Note: Sorry, moregames, some time ago I said that private methods had no speed difference compared to protected/public methods. This is wrong. Private methods can be inlined by the compiler and are therefore at least potentially faster.

Jacob Marner

Jacob Marner, M.Sc.Console Programmer, Deadline Games
One more speed tip.

If you have a method that does not use and member fields of its class and does not call any non-static methods in a class this the call to that method can be made faster by declaring it static. In static methods there is no "this" reference, so the call can be made faster, but of course assumes that you don''t need it.

Jacob Marner
Jacob Marner, M.Sc.Console Programmer, Deadline Games

This topic is closed to new replies.

Advertisement