Significance of final class in Java | Akshay Deo Significance of final class in Java · Akshay Deo

Significance of final class in Java


Hello guys, recently I have been working on Java for Android Application of AppSurfer. And while coding, specially for mobile devices, you need to be concerned about speed of your application. So this post is related with improving speed of execution to some extent using final classes whenever it is applicable.

When to use a final class :
When you are sure that the class is not going to be extended any further, then make that class as final class. When you make a class as final class, it does not allow any class to extend it.

What is the advantage of that
When you make a class as final class, then implicitly all of the methods inside it are final. So Java compiler is sure that those methods are not going to get overridden anywhere else. Java compiler may be able to inline final method then.

final class SquareMaker{
                public int getSquare(int input) {return input*input;}
        }

        public class test {
                public static void main(String args[])
                {
                        SquareMaker sqMkr = new SquareMaker();
                        System.out.println(""+ sqMkr.getSquare(10));
                }
        }

This would run about twice as fast when class Square maker is made final (http://www.glenmccl.com/).

Happy coding !! :)


powered by TinyLetter



Comments