Java/C3/Using-final-keyword/English
Title of script: USING FINAL KEYWORD
Author: Trupti Rajesh Kini
Keywords: video tutorial, final keyword, final variable, final method, final class
Visual Cue | Narration |
Slide 1 | Welcome to the spoken-tutorial on Using final keyword. |
Slide 2 | In this tutorial we will learn about the final keyword and when to invoke it.
|
Slide 3
Software Requirements
|
To record this tutorial, I am using
|
Slide 4
Prerequisites
|
To follow this tutorial, you must have,
If not, for relevant Java tutorials, please visit our website. |
Slide 5
final keyword |
First of all, we will learn what final keyword is.
It can be applied to variables, methods and classes. |
What is final variable?
Slide 6 |
Now, let us learn what final variable is.
That is, it will be a constant. |
Go to the Eclipse IDE >> Click on MyProject | Let us switch to Eclipse IDE now.
|
Go to the Employee class | So we will directly go to the Employee class. |
Point to the variable name | Come to the variable called name. |
Type final keyword | Type final keyword before the variable name.
We have made the variable name as final. |
Type sneha | Let us initialize the variable name with value sneha. |
Save and Run the program. | Now we will Save and Run the program. |
Highlight the error
cannot assign a value to final variable name name=newName; |
It gives a compilation error.
|
This is because here the final variable is already declared and initialised.
...within a program ...within a function ...within a method where?Actually here we are trying to say that final variable cannot be modified so its initialised once. Then, it can be initialised when we declare it or else in the constructor. This we have explained ahead.So, what I understand is it has to be initialised only once in a program.rightSo pls mention this clearly in the script.We can initialize a final variable only once. | |
Go to the Employee class
Point to the method setName Enclose the method inside /* */ |
We will go back to our code.
In the Employee class, we will comment the method setName. |
Go to TestEmployee class->main method
Type // before manager.setName("Nikkita Dinesh"); |
Go to TestEmployee class.
|
Save and Run the program. | Save and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Great!!!
We got the output as, Name: Sneha Email: abc@gmail.com Manager of: Accounts |
This is because we have initialized final variable only once. | |
Go to Employee class->variable name | Go to the Employee class.
Go to the final variable name. |
Remove the initialization “sneha” | Let us remove the initialization of final variable name.
So we will remove “sneha”. |
Go to the method setName
Uncomment the method. |
Let us uncomment the method setName.
|
Save and run the program. | And we will Save and Run the program once again. |
Highlight the error
cannot assign a value to final variable name name=newName; |
It gives a compilation error.
|
This is because if the final variable is not initialised, then only constructor can initialize it.
Not anywhere else in the program. | |
Go to Employee class | For that, let’s create a constructor in the Employee class.
|
We know that constructor has the same name as class name. | |
Type,
Employee() { name="Sneha"; } |
Type
Employee, parentheses, open and close curly brackets
|
Point to the method setName
Enclose the method inside /* */ |
Let us comment the method setName. |
Save and Run the program. | We will Save and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We get the output as,
Name: Sneha Email: abc@gmail.com Manager of: Accounts
|
final variable is successfully initialized in a constructor. | |
Now let us learn about final static variables. | |
Go to the Employee class | For that let us go back to the Employee class. |
Go to the variable name | Let us go to the variable name. |
Type static before final keyword | Let us add the keyword static before final keyword to make it static. |
Save & Run the program. | And we will once again Save and Run the program. |
Highlight the error
cannot assign a value to final variable name name="Sneha"; |
We get a compilation error.
“Cannot assign a value to static final variable name.” |
slides and document is not synchronized from this point and onward.
Further slide 9.1 and 9.2 [i.e. 25,26] contains one extra word block in second bullet,doneSlide 7 |
This is because static final variables cannot be initialized in the constructor.
|
Slide 8 | static variables are shared among all the objects of a class.
|
Go to Eclipse IDE | Let us switch back to Eclipse IDE. |
Go to the Employee class ->constructor
|
Let’s create a static block.
|
Save and Run the program. | Save and Run the program. |
Highlight the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We will get the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Now let’s use final variable as a parameter to the method. | |
Go to class Employee | Let us go to the Employee class. |
Go to the method setEmail | Let us go to the method setEmail. |
Type final before String newEmail | Add final keyword before String newEmail to make the parameter final. |
Save and Run the program. | Save and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We will get the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Back to Eclipse IDE.
Go to Employee class Go to the method setEmail |
Go to the Employee class.
Go to the method setEmail. |
Type newEmail="kinitrupti@gmail.com"; | Now, inside the method lets initialize newEmail variable.
Inside the method type, newEmail="abcIt is not recommended to give out personal email-ids in demos.
OR Use the spokentutorial official email-id.Done@gmail.com";
|
Save & Run the program. | Once again Save & Run the program. |
Highlight the error,
final parameter newEmail may not be assigned |
We will get a compilation error.
“final parameter newEmail may not be assigned” |
This is because final variable as parameter to a method cannot be modified by that method.
| |
Now let’s learn about final method. | |
Go to Employee class
Go to the method setEmail |
Come back to the Employee class.
Go to the method setEmail. |
Point to the newEmail initialization
remove newEmail="kinitrupti@gmail.com"; |
Let’s remove the newEmail initialization. |
Go to the method getDetails | Go to the method getDetails. |
Type final keyword before getDetails. | Add final keyword before getDetails to make it final. |
Save and Run the program. | And as before, Save and Run the program. |
Point to the compilation error,
Cannot override the final method from Employee |
We get compilation error,
“Cannot override the final method from Employee.” |
This is because if you make any method as final, you cannot override it. | |
Slide 9 | What if final method is private?
|
Go to Eclipse IDE | Let us switch back to Eclipse IDE. |
Go to Employee class | Now, let us see whether constructor can be declared as final or not.
Come to the Employee class. |
Type,
Employee() { name="Sneha"; }
|
Let us create a constructor again as Employee.
Type Employee, parentheses, open and close curly brackets
|
Remove static keyword. | |
Type final before the constructor | Add final before the constructor. |
Save and run the program. | Save and run the program. |
Highlight compilation error,
modifier final not allowed here |
We get a compilation error once again.
“modifier final not allowed here” |
This is because constructor cannot be final since constructors are not inherited. | |
Next, let us learn about final class. | |
Go the Employee class | Go to the Employee class. |
Type final keyword before the class Employee. | Add final keyword before the class Employee. |
Save and Run the program. | Save and Run the program. |
Highlight the compilation error,
The type Manager cannot subclass the final class Employee |
We get the compilation error again. The error msg says-
“The type Manager cannot subclass the final class Employee.”
|
Slide 10 | This is because if you make any class as final, you cannot extend it. |
Remove the keyword final before Employee class | Remove the keyword final before Employee class. |
Save and Run the program. | Now Save and Run the program. |
Highlight the output,
|
This time we get the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts
|
Let us summarize. | |
Slide 11
Summary
|
In this tutorial we have learnt:
|
Slide 12
Assignment
|
As an assignment
|
Slide 13
About Spoken Tutorial Project
|
Watch the video available at the following link http://spoken-tutorial.org/What is a Spoken Tutorial
|
Slide 14
About Spoken Tutorial workshops
|
The Spoken Tutorial Project Team
|
Slide 15
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. |