Java/C3/Using-final-keyword/English
Title of script: USING FINAL KEYWORD
Author: Trupti Rajesh Kini
Keywords:
|
|
---|---|
Slide 1 | Welcome to the spoken-tutorial on Using final keyword.
|
Slide 2 | In this tutorial we will learn :
When to invoke final keyword. final keyword in various contexts:
|
Slide 3
Software Requirements
|
Here we are using
|
Slide 4
Prerequisites
|
To follow this tutorial, you must have,
Knowledge of basics of Java and Eclipse IDE. Knowledge of Subclassing and Method overriding . If not, for relevant Java tutorials, please visit our website.
|
Slide 5
final keyword |
First of all, we will learn what final keyword is.
The final keyword in java is used to restrict the user.
|
What is final variable?
Slide 6 |
Now, let us learn what is final variable?
If you make any variable as final, you cannot change the value of final variable.
|
Go to the Eclipse IDE >> Click on MyProject | I have already created a project named MyProject in Subclassing and Method overriding tutorial. |
Point to Employee class | Let us come to the Employee class. |
Point to the variable 'name' | Come to the variable name. |
Type final keyword | Type final keyword before the variable name. |
Save & run the program. | |
Point to the error | It gives a compilation error,
cannot assign a value to final variable name name=newName; |
Slide 7 | This is because,
final variable if declared and not initialised then, It can be initialised by the constructor only.
|
For that, lets create a constructor.
We have already learnt what is a constructor. | |
We know that constructor has the same name as class name. | |
Point to the constructor Employee | Type Employee,
Parentheses, Open curly brackets, Inside we will declare name as Sneha. |
Point to the method setName | Let us comment the method setName by enclosing in /* */. |
Point to TestEmployee class | Go to TestEmployee class.
Go to the main method and comment instance for the method manager.setName("Nikkita Dinesh"); |
Save and run the program. | |
Point to the error | We get a compilation error,
cannot assign a value to final variable name name="Sneha"; |
Slide 8 | This is because,
You can only initialize a final variable once. |
Come back to the Employee class. | |
Point to the final variable name | Remove the blank initialized in the declaration of name. |
Save and run the program. | |
Point to output | Great!!!
We got output as , Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Now let us learn about final static variables. | |
Point to the Employee class | For that let us go to the Employee class. |
Point to the variable name | Let us go to the variable name.
Let us add keyword static before final keyword. |
Save & run the program. | |
Point to the error | We get a compilation error,
cannot assign a value to final variable name name="Sneha"; |
Slide 9 | This is because,
static final variables cannot be assigned value in constructor; they must be assigned a value with their declaration OR they must be declared ina static block. |
Slide 10 | static variables are shared among all the objects of a class.
Creating a new object would change the static variable. This is not allowed if the static variable is final. |
Point to the constructor | Let us change the constructor name to static to create a static block. |
Point to the output | Save and run the program.
We will get the output, Name: Sneha Email: abc@gmail.com Manager of: Accounts
|
Now lets use final variable as parameter to the method. | |
Point to class Employee | Let us go to the class Employee |
Point to the method setEmail | Let us go to the method setEmail.
Add final keyword before String newEmail. Save and run the program. |
Point to the output | We will get the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Type newEmail="kinitrupti@gmail.com"; | Now lets type inside the method,
newEmail="kinitrupti@gmail.com"; Save & run the prorgam. |
Point to the error | We will get a compilation error,
final parameter newEmail may not be assigned |
Slide 11 | This is because,
Value of final variable as parameter to a method cannot be changed by that method. |
Now lets learn about final method. | |
Point to the newEmail initialization | Lets remove the newEmail initialization. |
Slide 12 | If you make any method as final, you cannot override it. |
Point to the Employee class | Come to the Employee class. |
Point to the method getDetails | Come to the method getDetails. |
Add final keyword before getDetails. | |
Save and runAfter saving and running i dnt get compilation error. I get exception that final method cannot be overridden. Is it right? | |
What if final method is private?
Slide 13 |
What if final method is private?
Since private methods are inaccessible, they are implicitly final in Java. So adding final specifier to a private method doesn’t add any value. |
Point to Employee class | Let us try this out.
Come to the Employee class. Let us create a constructor again as Employee. Add final before the constructor. |
Point to the compilation error | Save and run the program.
We get a compilation error, modifier final not allowed here |
Slide 14 | This is because,
constructor cannot be final since constructors are not inherited.
|
Now lets learn about final class.
| |
Point to the Employee class | Come to the Employee class.
Add final keyword before the class name. Save and run the program.
|
Does not give a compilation error instead throws an exception during execution. Is it right? Try this out with the codes i have attached. | |
Slide 15 | This is because,
If you make any class as final, you cannot extend it. |
Slide 7
Summary
|
In this tutorial we have learnt:
|
Slide 8
Assignment
|
Assignment
|
Slide 9
About Spoken Tutorial Project
|
Watch the video available at the following link
|
Slide 10
About Spoken Tutorial workshops
|
The Spoken Tutorial Project Team
|
Slide 11
Acknowledgement
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
This is Trupti Kini from IIT Bombay signing off. Thanks for joining.
|