Difference between revisions of "Java/C3/Using-final-keyword/English"
Line 53: | Line 53: | ||
* Basic knowledge of '''Java''' and''' Eclipse IDE.''' | * Basic knowledge of '''Java''' and''' Eclipse IDE.''' | ||
− | * and knowledge of''' Subclassing''' and''' Method overriding'''. | + | * and knowledge of '''Subclassing''' and '''Method overriding'''. |
If not, for relevant''' Java''' tutorials, please visit our website. | If not, for relevant''' Java''' tutorials, please visit our website. | ||
Line 61: | Line 61: | ||
'''final''' keyword | '''final''' keyword | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| First of all, we will learn what''' final''' '''keyword''' is. | + | | style="border:1pt solid #000000;padding:0.097cm;"| First of all, we will learn what '''final''' '''keyword''' is. |
− | + | '''final''' is a '''keyword''' or '''reserved''' word in '''Java'''. | |
− | ''' | + | |
It can be applied to '''variables, methods '''and''' classes.''' | It can be applied to '''variables, methods '''and''' classes.''' | ||
Line 81: | Line 80: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the''' Eclipse IDE >> '''Click on '''MyProject''' | | style="border:1pt solid #000000;padding:0.097cm;"| Go to the''' Eclipse IDE >> '''Click on '''MyProject''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| I am switching to '''Eclipse IDE '''now. |
Line 88: | Line 87: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the''' Employee '''class | | style="border:1pt solid #000000;padding:0.097cm;"| Go to the''' Employee '''class | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| So we will directly go to the '''Employee class'''. | + | | style="border:1pt solid #000000;padding:0.097cm;"| So we will directly go to the '''Employee class''' of the project. |
|- | |- | ||
Line 96: | Line 95: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' final keyword''' | | style="border:1pt solid #000000;padding:0.097cm;"| Type''' final keyword''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Add ''' final''' '''keyword''' before the variable''' name'''. |
We have made the variable '''name '''as '''final.''' | We have made the variable '''name '''as '''final.''' | ||
Line 102: | Line 101: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' sneha''' | | style="border:1pt solid #000000;padding:0.097cm;"| Type''' sneha''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We will initialize the variable '''name '''with value '''sneha.''' |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We will '''Save''' and '''Run''' the program. |
|- | |- | ||
Line 114: | Line 113: | ||
'''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>''' | '''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We get compilation error. |
− | ''' | + | '''The final field Employee.name cannot be assigned''' |
|- | |- | ||
Line 123: | Line 122: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| This is because here the '''final''' '''variable''' is already declared and initialised. | | style="border:1pt solid #000000;padding:0.097cm;"| This is because here the '''final''' '''variable''' is already declared and initialised. | ||
+ | We can initialize a final variable only once. | ||
− | + | So we will comment the method '''setName''' which modifies the variable '''name'''. | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Save the class | |
|- | |- | ||
Line 150: | Line 132: | ||
Type // before '''manager.setName("Nikkita Dinesh"); ''' | Type // before '''manager.setName("Nikkita Dinesh"); ''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now, come to''' TestEmployee''' '''class'''. |
− | + | Come to the '''main''' '''method''' and comment the line''' manager.setName("Nikkita Dinesh");''' | |
Line 166: | Line 148: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now lets '''Save''' the class and '''Run''' the program. |
|- | |- | ||
Line 178: | Line 160: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Great!!! | | style="border:1pt solid #000000;padding:0.097cm;"| Great!!! | ||
− | We got the output | + | We got the output, |
'''Name: Sneha''' | '''Name: Sneha''' | ||
Line 188: | Line 170: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| | | style="border:1pt solid #000000;padding:0.097cm;"| | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We got this output as we already initialized variables with these values in TestEmployee class and Employee class. |
|- | |- | ||
Line 194: | Line 176: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee '''class. | | style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee '''class. | ||
− | + | Now come to the '''final variable''' '''name.''' in Employee class. | |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the initialization''' “sneha”''' | | style="border:1pt solid #000000;padding:0.097cm;"| Remove the initialization''' “sneha”''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Remove the initialization of '''final''' '''variable''' '''name.''' |
− | + | That is remove''' “sneha”.''' | |
− | + | ||
|- | |- | ||
Line 206: | Line 187: | ||
Uncomment the method. | Uncomment the method. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Uncomment the method''' setName'''. |
Line 213: | Line 194: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''run''' the program. | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''run''' the program. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. |
|- | |- | ||
Line 221: | Line 202: | ||
'''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>''' | '''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| We get the error, |
− | + | ||
− | + | '''The final field Employee.name cannot be assigned''' | |
|- | |- | ||
Line 230: | Line 210: | ||
| style="border:1pt solid #000000;padding:0.097cm;"| This is because if the '''final''' variable is not initialised, then only''' constructor''' can initialize it. | | style="border:1pt solid #000000;padding:0.097cm;"| This is because if the '''final''' variable is not initialised, then only''' constructor''' can initialize it. | ||
− | + | That is it cannot be modified anywhere else in the program. | |
|- | |- | ||
Line 249: | Line 229: | ||
''' }''' | ''' }''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| SO we will type |
'''Employee, parentheses, open and close curly brackets''' | '''Employee, parentheses, open and close curly brackets''' | ||
− | + | and inside the curly brackets, lets initialize the variable''' name''' with value ''' Sneha.''' | |
|- | |- | ||
Line 260: | Line 240: | ||
Enclose the method inside''' '''/* */ | Enclose the method inside''' '''/* */ | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Comment the '''method setName'''. |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. |
|- | |- | ||
Line 274: | Line 254: | ||
'''Manager of: Accounts''' | '''Manager of: Accounts''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| We | + | | style="border:1pt solid #000000;padding:0.097cm;"| We got the desired output. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
|- | |- | ||
Line 291: | Line 263: | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| | | style="border:1pt solid #000000;padding:0.097cm;"| | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Now | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now we will learn about''' final static variables.''' |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee class''' | | style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee class''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Come to the final variable in ''' Employee class'''. |
+ | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' static '''before''' final keyword''' | | style="border:1pt solid #000000;padding:0.097cm;"| Type''' static '''before''' final keyword''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Add '''static''' keyword before''' final''' '''keyword''' |
+ | |||
+ | We have made the final variable as static. | ||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' & '''Run''' the program. | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' & '''Run''' the program. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save '''and '''Run''' the program. |
|- | |- | ||
Line 315: | Line 287: | ||
'''name<nowiki>="</nowiki>Sneha";''' | '''name<nowiki>="</nowiki>Sneha";''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| We get | + | | style="border:1pt solid #000000;padding:0.097cm;"| We get error. |
− | + | '''The final field Employee.name cannot be assigned''' | |
− | ''' | + | |
|- | |- | ||
Line 339: | Line 310: | ||
− | This is not allowed if the''' static''' '''variable''' is | + | This is not allowed if the''' static''' '''variable''' is final |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Eclipse IDE''' | | style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Eclipse IDE''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Switch back to '''Eclipse IDE.''' |
|- | |- | ||
Line 350: | Line 321: | ||
Delete '''Employee '''and type '''static'''. | Delete '''Employee '''and type '''static'''. | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| So now we will create a''' static block.''' |
− | For that, in the '''Employee '''class, go to the '''constructor. ''' | + | For that, in the '''Employee '''class, go to the '''constructor. ''' we created |
− | Here instead of '''Employee(), '''type '''static'''. | + | Here instead of '''Employee(), we will '''type '''static'''. |
+ | We have created a static block. | ||
|- | |- | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now we will '''Save''' and '''Run''' the program. |
− | | style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program. | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now we will '''Save''' and '''Run''' the program. |
|- | |- | ||
Line 370: | Line 342: | ||
'''Manager of: Accounts''' | '''Manager of: Accounts''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| We | + | | style="border:1pt solid #000000;padding:0.097cm;"| We got the desired output, |
+ | We have successfully initialized static final variable. | ||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| | | style="border:1pt solid #000000;padding:0.097cm;"| | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Now let’s use''' final''' variable as a parameter to the '''method'''. | | style="border:1pt solid #000000;padding:0.097cm;"| Now let’s use''' final''' variable as a parameter to the '''method'''. | ||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the method''' setEmail''' | | style="border:1pt solid #000000;padding:0.097cm;"| Go to the method''' setEmail''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| | + | | style="border:1pt solid #000000;padding:0.097cm;"| Come to the method ''' setEmail.''' in Employee class. |
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''before '''String newEmail''' | | style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''before '''String newEmail''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Add''' final''' '''keyword''' before''' String newEmail''' | + | | style="border:1pt solid #000000;padding:0.097cm;"| Add''' final''' '''keyword''' before''' String newEmail''' |
+ | We have made the parameter as final. | ||
|- | |- | ||
Line 406: | Line 371: | ||
'''Manager of: Accounts''' | '''Manager of: Accounts''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| We | + | | style="border:1pt solid #000000;padding:0.097cm;"| We got the desired output, |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''newEmail<nowiki>="</nowiki>kinitrupti@gmail.com";''' | | style="border:1pt solid #000000;padding:0.097cm;"| Type '''newEmail<nowiki>="</nowiki>kinitrupti@gmail.com";''' | ||
− | | style="border:1pt solid #000000;padding:0.097cm;"| Now, | + | | style="border:1pt solid #000000;padding:0.097cm;"| Now, come to the method setEmail. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | Inside the '''method''' we will type, | |
− | + | newEmail is equal to inside double quotes abc@gmail.com semicolon. | |
Revision as of 12:18, 5 August 2014
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.
final is a keyword or reserved word in Java. 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 | I am switching to Eclipse IDE now.
|
Go to the Employee class | So we will directly go to the Employee class of the project. |
Point to the variable name | Come to the variable called name. |
Type final keyword | Add final keyword before the variable name.
We have made the variable name as final. |
Type sneha | We will initialize the variable name with value sneha. |
Save and Run the program. | We will Save and Run the program. |
Highlight the error
cannot assign a value to final variable name name=newName; |
We get compilation error.
|
This is because here the final variable is already declared and initialised.
We can initialize a final variable only once. So we will comment the method setName which modifies the variable name. Save the class | |
Go to TestEmployee class->main method
Type // before manager.setName("Nikkita Dinesh"); |
Now, come to TestEmployee class.
|
Save and Run the program. | Now lets Save the class and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
Great!!!
We got the output, Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We got this output as we already initialized variables with these values in TestEmployee class and Employee class. | |
Go to Employee class->variable name | Go to the Employee class.
Now come to the final variable name. in Employee class. |
Remove the initialization “sneha” | Remove the initialization of final variable name.
That is remove “sneha”. |
Go to the method setName
Uncomment the method. |
Uncomment the method setName.
|
Save and run the program. | Save and Run the program. |
Highlight the error
cannot assign a value to final variable name name=newName; |
We get the error,
The final field Employee.name cannot be assigned |
This is because if the final variable is not initialised, then only constructor can initialize it.
That is it cannot be modified 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"; } |
SO we will type
Employee, parentheses, open and close curly brackets
|
Point to the method setName
Enclose the method inside /* */ |
Comment the method setName. |
Save and Run the program. | Save and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We got the desired output.
|
final variable is successfully initialized in a constructor. | |
Now we will learn about final static variables. | |
Go to the Employee class | Come to the final variable in Employee class.
|
Type static before final keyword | Add static keyword before final keyword
We have made the final variable as static. |
Save & Run the program. | Save and Run the program. |
Highlight the error
cannot assign a value to final variable name name="Sneha"; |
We get error.
The final field Employee.name cannot be assigned |
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 | Switch back to Eclipse IDE. |
Go to the Employee class ->constructor
|
So now we will create a static block.
We have created a static block. |
Now we will Save and Run the program. | Now we will Save and Run the program. |
Highlight the output,
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We got the desired output,
We have successfully initialized static final variable.
|
Now let’s use final variable as a parameter to the method. | |
Go to the method setEmail | Come to the method setEmail. in Employee class. |
Type final before String newEmail | Add final keyword before String newEmail
We have made the parameter as final. |
Save and Run the program. | Save and Run the program. |
Highlight the output
Name: Sneha Email: abc@gmail.com Manager of: Accounts |
We got the desired output,
|
Type newEmail="kinitrupti@gmail.com"; | Now, come to the method setEmail.
Inside the method we will type, newEmail is equal to inside double quotes abc@gmail.com semicolon.
|
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. |