Copyright

Java: Final Keyword

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

...And that's final! The final keyword in Java is a simple tool, but it is the programming equivalent of these words. This lesson will cover the concept of Final and provide some real-world examples how how to use the final keyword to protect and clean Java code.

That's Final!

In fact, final in Java is a programmer's way of stating exactly that. It's a powerful little statement that helps us create tighter, more readable code. It's helpful in reducing logic errors and unintended use of, variables, classes and methods. It also improves program security and prevents unwanted use of your code.

The final keyword can be used on classes, methods, and variables: Starting from the most restrictive in a program (classes), and moving down to the more granular elements, the final protects your code.

Classes

The most significant feature of the final keyword is that offers security.

A subclass cannot be created from a class that is a final class. A subclass is a class built from another class. For example, if we had a class called 'Dog' that is a subclass of a class called 'Animal', it would inherit all the properties of 'Animal'.

Hackers can use the concept of the subclass to their advantage. By creating a subclass, they can then create a class that looks just like the original. Except that the code could be used to steal data or damage computer systems.

To get around this, create classes with the help of the final keyword, and subclasses will not be created from them.

For this reason, the Java string class is set to final: You can't make it into anything else, as hard as you try. String is so powerful, and so important to core Java functionality, that it is protected. You should treat your own classes this way, also. It's fine to have a mix of final and public classes, but remember that a final class protects it from be used as a template for a subclass.

Declaring a final class is as easy as:


final class Employee {
 //code here
}


Now, if we try to create a subclass of the Employee class:


final class Employee {
 private float payRate = 10;
}
private class UnionEmployee extends employee {
}


The error in the compiler will be something like: ''Cannot inherit from Final Employee''. A final class protects that class, and the code won't even compile, keeping it safe form hackers.

Methods

Sometimes you don't need to to protect the entire class, since the power of object-oriented programming is the ability to create subclasses. Instead, you can simply create some methods that you need to protect from other classes/subclasses.

We don't have to make the Employee class final (since we probably would want a UnionEmployee class); instead we can make a method private. A good example would be getPayRate, as seen here:


public class Employee {
 final void getPayRate(Employee, payRate, hours){
  //code here
 }
}


Variables

Any local variables should be final, if the variable is not going to be used outside of a method. Once final, the variable can't be re-initialized. It tells the programmer, and other future programmers, that the intent was to keep the variable local and protected.

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support