Difference between revisions of "Java/C3/Using-final-keyword/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Title of script''': '''USING FINAL KEYWORD'''
+
'''Title of script: USING FINAL KEYWORD '''
  
'''Author: Trupti Rajesh Kini'''
+
'''Author: Trupti Rajesh Kini'''
 +
 
 +
'''Keywords: video tutorial, final keyword, final variable, final method, final class'''  
  
'''Keywords: '''
 
  
|-
 
  
 
{| style="border-spacing:0;"
 
{| style="border-spacing:0;"
! <center>Visual Clue</center>
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Visual Cue'''
! <center>Narration</center>
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Narration'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 1'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 1  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on '''Using final keyword.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on '''Using final keyword.'''  
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 2
 +
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn about
  
 +
The final keyword and when to invoke it.
  
 +
We will also learn about -
 +
 +
* '''final variables,'''
 +
* '''final methods,'''
 +
* '''final classes'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 2'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 3
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we will learn :
+
  
When to invoke final keyword.
+
'''Software Requirements'''
  
'''final''' keyword in various contexts:
 
  
* '''variable, '''
 
* '''method, '''
 
* '''class'''
 
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| To record this tutorial, I am using
  
 +
* '''Ubuntu Linux''' version 12.04
 +
* '''JDK''' 1.7
 +
* '''Eclipse''' 4.3.1
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 3 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 4
  
'''Software Requirements'''  
+
'''Pre-requisites'''  
  
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we are using
+
| style="border:1pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have,
 
+
* '''Ubuntu''' '''Linux '''version 12.04
+
* '''JDK '''1.7
+
* '''Eclipse '''4.3.1
+
  
 +
* Basic knowledge of '''Java''' and '''Eclipse IDE.'''
 +
* and knowledge of '''Subclassing''' and '''Method overriding'''.
  
 +
If not, for relevant '''Java''' tutorials, please visit our website.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 4 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 5
  
'''Prerequisites'''  
+
'''final''' keyword
 +
| 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 '''or '''classes.'''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| To follow this tutorial, you must have,
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''What is final variable?'''
  
Knowledge of basics of '''Java''' and '''Eclipse IDE.'''  
+
Slide 6
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us learn what '''final''' '''variable''' is.
  
Knowledge of '''Subclassing''' and '''Method overriding '''.
+
'''final variable''' is a variable whose value cannot be changed.  
  
If not, for relevant '''Java '''tutorials, please visit our website.
+
That is, it will be a '''constant'''.  
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Eclipse IDE '''
  
 +
Point to '''MyProject'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| I am switching to '''Eclipse IDE '''now.
  
 +
I had already created a project named '''MyProject''' in the previous tutorial.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 5 '''
+
| 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''' of the project.
  
'''final''' keyword
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| First of all, we will learn what '''final''' keyword''' '''is.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Point to the variable '''name'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the variable '''name.'''  
  
The '''final keyword''' in java is used to restrict the user.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final keyword'''  
  
 +
Highlight variable '''name'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final keyword''' before the variable '''name'''.
  
 +
We have made the variable '''name '''as '''final.'''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''sneha'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will initialize the variable '''name '''with value '''sneha.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''What is final variable?'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will '''Save''' and '''Run''' the program.
  
'''Slide 6'''
+
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, let us learn what is '''final '''variable?
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error
  
If you make any variable as '''final''', you cannot change the value of '''final '''variable.
+
'''The final field Employee.name cannot be assigned'''  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get compilation error.  
  
 
+
'''The final field Employee.name cannot be assigned'''
i.e It will be constant.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Go to the Eclipse IDE >> Click on MyProject'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have already created a project named '''MyProject '''in '''Subclassing and Method overriding '''tutorial.
+
  
|-
+
'''public final string name <nowiki>= “sneha”</nowiki>'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is because here the '''final variable''' name is already declared and initialised.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|
+
  
|-
+
We can initialize a final variable only once.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to Employee class'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us come to the '''Employee class'''.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the variable 'name''''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Comment the method '''setName'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the variable '''name.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So we will comment the method '''setName''' which modifies the variable '''name'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type final keyword'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save the class.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type '''final '''keyword before the variable '''name'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save the '''class'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''TestEmployee class->main '''method
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save & run the program.
+
  
|-
+
Type // before '''manager.setName("Nikkita Dinesh"); '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the error'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now, come to '''TestEmployee class'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| It gives a compilation error,
+
 
 +
Come to the '''main method''' and comment the line '''manager.setName("Nikkita Dinesh");'''
 +
 
 +
We commented this line as it was instance of method '''setName.'''
  
'''cannot assign a value to final variable name '''
+
We have already commented '''setName '''method in '''Employee '''class.
  
'''name=newName; '''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 7'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us '''Save''' the class and '''Run''' the program.
  
'''final''' variable if declared and not initialised then,
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the output
  
It can be initialised by the '''constructor only'''.
+
'''Name: Sneha'''  
  
 +
'''Email: abc@gmail.com'''
  
 +
'''Manager of: Accounts'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Great!!!
  
 +
We got the output,
 +
 +
'''Name: Sneha'''
 +
 +
'''Email: abc@gmail.com'''
 +
 +
'''Manager of: Accounts'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the initialization in '''TestEmployee''' class and
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For that, lets create a '''constructor.'''
+
  
We have already learnt what is a '''constructor.'''
+
'''Employee '''class.
 +
| 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'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Employee''' class'''->final '''variable '''name'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We know that '''constructor''' has the same name as class name.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now come to the '''final variable''' '''name.''' in''' Employee '''class.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the constructor Employee'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the initialization “'''sneha”'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type Employee,
+
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the initialization of '''final variable name.'''
  
Parentheses,
+
That is remove “'''sneha”.'''
  
Open curly brackets,
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the method '''setName'''
 +
 
 +
Uncomment the method.
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Uncomment the method '''setName'''.
  
Inside we will declare '''name''' as '''Sneha.'''
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the method setName'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''run''' the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us comment the method '''setName '''by enclosing in '''/* */'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to TestEmployee class'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Go to '''TestEmployee''' class.
+
  
Go to the main method and comment instance for the method '''manager.setName("Nikkita Dinesh");'''
+
'''The final field Employee.name cannot be assigned'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get the error,
 +
 
 +
'''The final field Employee.name cannot be assigned'''  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save and run the program.
+
| 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.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the error'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Employee '''class
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get a compilation error,
+
| style="border:1pt solid #000000;padding:0.097cm;"| For that, let’s create a '''constructor '''in the '''Employee class.'''
  
'''cannot assign a value to final variable name '''
+
We have already learnt what a '''constructor '''is earlier.
  
'''name="Sneha"; '''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We know that '''constructor''' has the same name as '''class''' name.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 8'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type,  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
  
You can only initialize a '''final''' variable once.
+
'''Employee() { '''
  
|-
+
'''name="Sneha";'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|
+
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to the '''Employee''' class.
+
'''}'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| So we will type
 +
 
 +
'''Employee, parentheses, open and close curly brackets'''  
 +
 
 +
and inside the curly brackets, lets initialize the variable '''name''' with value '''sneha semicolon .'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the final variable name'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Point to the method '''setName'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Remove the blank initialized in the declaration of '''name.'''
+
 
 +
Comment the method
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Comment the '''method setName'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right: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-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to output'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the output  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Great!!!
+
  
We got output as ,
+
'''Name: Sneha'''
  
Name: Sneha
+
'''Email: abc@gmail.com'''
  
Email: abc@gmail.com
+
'''Manager of: Accounts'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We got the desired output.  
  
Manager of: Accounts
 
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"|
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us learn about '''final static variables.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''final '''variable is successfully initialized in a '''constructor'''.
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now we will learn about '''final static variables.'''  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the Employee class'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee class'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| For that let us go to the '''Employee class'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the final variable in '''Employee class'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the variable name'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''static '''before '''final keyword'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go to the variable '''name'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''static''' keyword before '''final keyword'''.  
  
Let us add keyword '''static''' before '''final '''keyword.
+
We have made the '''final variable''' as '''static'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' & '''Run''' the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save & run the program.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save '''and '''Run''' the program.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the error'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get a compilation error,
+
  
'''cannot assign a value to final variable name '''
+
'''The final field Employee.name cannot be assigned'''  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get error.
  
'''name="Sneha";'''
+
'''The final field Employee.name cannot be assigned'''  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 9 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 7
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is because '''static final variables '''cannot be initialized in the '''constructor.'''
  
'''static final''' variables cannot be assigned value in '''constructor;''' they must be assigned a value with their declaration OR
+
They must be assigned a value with their declaration.
  
they must be declared ina '''static block'''.
+
Or they must be declared in a '''static block'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 10'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 8
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''static''' variables are shared among all the objects of a class.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''static''' variables are shared among all the objects of a '''class'''.  
  
Creating a new object would change the''' static''' variable.
+
Creating a new '''object''' would change the '''static variable'''.  
  
This is not allowed if the '''static''' variable is final.
+
This is not allowed if the '''static variable''' is '''final.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the constructor'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Eclipse IDE'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us change the constructor name to '''static '''to create a '''static block.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to '''Eclipse IDE.'''  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the output'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee  class ->Employee()'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save and run the program.
+
  
We will get the output,
+
Delete '''Employee() '''and type '''static'''.
  
'''Name: Sneha '''
+
Highlight '''static''' block
 +
| style="border:1pt solid #000000;padding:0.097cm;"| So now we will create a '''static block.'''  
  
'''Email: abc@gmail.com '''
+
For that, in the '''Employee '''class, come to the '''constructor. '''we created.
  
'''Manager of: Accounts '''
+
Here instead of '''Employee(), '''we will type '''static'''.
  
 +
We have created a '''static block'''.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now we will '''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;"| Highlight the output,
 +
 +
'''Name: Sneha'''
 +
 +
'''Email: abc@gmail.com'''
 +
 +
'''Manager of: Accounts'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
We got the desired output,
 +
 +
We have successfully initialized '''static final''' variable.
  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now lets use '''final '''variable as 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-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to class Employee'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the method '''setEmail'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go to the class '''Employee'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the method '''setEmail.''' in '''Employee''' class.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the method setEmail'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''before '''String newEmail'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us go to the method '''setEmail.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final''' keyword before '''String newEmail'''  
  
Add''' final''' keyword before '''String newEmail'''.
+
We have made the parameter as '''final'''.  
  
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-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the output'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the output  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get the output,
+
  
'''Name: Sneha '''
+
'''Name: Sneha'''  
  
'''Email: abc@gmail.com '''
+
'''Email: abc@gmail.com'''  
  
'''Manager of: Accounts '''
+
'''Manager of: Accounts'''  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We got the desired output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Type newEmail="kinitrupti@gmail.com";'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the method '''setEmail'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now lets type inside the method,
+
  
'''newEmail="kinitrupti@gmail.com";'''
+
Type '''newEmail="abc@gmail.com";'''  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, come to the method''' setEmail'''.
  
Save & run the prorgam.
+
Inside the '''method''' we will type,
  
|-
+
'''newEmail is equal to abc@gmail.com semicolon'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the error'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will get a compilation error,
+
  
'''final parameter newEmail may not be assigned'''
+
We have modified the '''final''' variable '''newEmail'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 11'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' & '''Run''' the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
| style="border:1pt solid #000000;padding:0.097cm;"| Once again we will '''Save''' & '''Run '''the program.  
 
+
Value of '''final '''variable as parameter to a method cannot be changed by that method.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error,
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now lets learn about '''final''' method.
+
  
|-
+
'''The final local variable newEmail cannot be assigned. '''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the newEmail initialization'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| We get error.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Lets remove the '''newEmail''' initialization.
+
  
|-
+
'''The final local variable newEmail cannot be assigned.'''  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 12'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| If you make any method as final, you cannot '''override''' it.
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the Employee class'''
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the '''Employee''' class.
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is because '''final''' variable as parameter to a '''method '''cannot be modified by that '''method.'''
  
|-
+
So lets remove the variable modification.  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the method getDetails'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the method '''getDetails.'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Add '''final '''keyword before '''getDetails.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now we will learn about '''final method'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the '''method getDetails''' in '''employee class'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save and runAfter saving and running i dnt get compilation error. I get exception that final method cannot be overridden. Is it right?
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to the '''method getDetails''' in '''employee class'''.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''What if final method is private?'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Type final keyword before '''getDetails.'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final '''keyword before '''method getDetails'''.
  
'''Slide 13'''
+
We have made the method as '''final'''.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| What if final method is '''private'''?
+
  
Since '''private''' methods are inaccessible, they are implicitly final in Java.
+
|-
 
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
So adding '''''final''' ''specifier to a '''private''' method doesn’t add any value.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save '''and '''Run''' the program.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to Employee class'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Point to the compilation error,
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us try this out.
+
  
Come to the '''Employee''' class.
+
'''class Manager overrides final method getDetails.() '''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get error,
  
Let us create a constructor again as '''Employee.'''
+
'''class Manager overrides final method getDetails.()'''  
  
Add final before the constructor.
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight method '''getDetails()''' in '''Manager''' class.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the '''method getDetails()''' in '''Manager''' class.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the compilation error'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight method '''getDetails()''' in '''Manager''' class.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Save and run the program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because if you make any '''method''' as '''final''', you cannot '''override''' it.
  
We get a compilation error,
+
'''Manager class method getDetails''' overrides '''getDetails method''' in '''Employee class. '''  
 
+
'''modifier final not allowed here'''
+
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 14'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 9
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
| style="border:1pt solid #000000;padding:0.097cm;"| What if '''final method''' is '''private'''?
  
'''constructor''' cannot be '''final''' since constructors are not inherited.
+
'''private methods''' are not inherited by the '''child class.'''  
  
 +
So we can add a method '''getDetails() '''in the '''child class'''.
  
 +
You can try this as an assignment.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Eclipse IDE'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Switch back to '''Eclipse IDE.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Employee''' class >>
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now lets learn about '''final class.'''
+
  
 +
remove '''final''' keyword>>
  
 +
Remove '''static''' keyword
 +
| style="border:1pt solid #000000;padding:0.097cm;"| In '''Employee''' class, remove '''final''' keyword before '''method getDetails'''.
  
 +
Remove '''static''' keyword before '''final''' variable name.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Point to the Employee class'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Instead of static'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the '''Employee '''class.
+
  
Add '''final '''keyword before the class name.
+
'''Type,'''  
  
Save and run the program.
+
'''Employee() '''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, we will learn whether '''constructor''' can me declared as''' final '''or not.  
  
 +
For that we will again create a '''constructor'''.
  
 +
So instead of '''static''' we will type, '''Employee parentheses'''.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''before the constructor
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final '''keyword before the '''constructor'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''run '''the program.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Does not give a compilation error instead throws an exception during execution. Is it right? Try this out with the codes i have attached.
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''run''' the program.  
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 15'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight compilation error,  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is because,
+
  
If you make any class as final, you cannot extend it.
+
'''Illegal modifier for the constructor in type Employee'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get error
 +
 
 +
'''Illegal modifier for the constructor in type Employee'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 7 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the''' final''' keyword before '''Employee()'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is because, '''constructor''' cannot be '''final''' since '''constructors''' cannot not be inherited.
  
'''Summary'''  
+
We will remove the '''final''' keyword before the '''constructor'''.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, we will learn about '''final class.'''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''keyword before the class '''Employee.'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final''' '''keyword''' before the '''class Employee.''' to make it '''final'''.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we have learnt:
+
|-
 +
| 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.
  
* When to invoke final keyword.
+
|-
* '''final''' keyword in various contexts:
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the compilation error,  
** '''variable, '''
+
** '''method, '''
+
** '''class'''
+
  
 +
'''The method setEmail is undefined for the type Manager'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get an error,
  
 +
'''The method setEmail is undefined for the type Manager'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 8 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Come to '''TestEmployee '''class and comment the lines.
  
'''Assignment'''  
+
'''manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); '''
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| To know the actual error, lets come to '''TestEmployee '''class and comment the lines.
  
 +
'''manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); '''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Assignment
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Save''' the class and''' run''' the program
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Save''' the class and '''run''' the program
  
* Repeat the same as shown in the tutorial for the '''Bike '''and''' Vehicle class.'''<br/>
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight the compilation error,
  
 +
'''The type Manager cannot subclass the final class Employee'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The actual error is
  
 +
'''The type manager cannot subclass the final class Employee.'''
  
 +
Here '''Manager class''' extends '''Employee class.'''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 9 '''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come back to '''Employee''' class and remove the''' final''' keyword
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| So let's come back to''' Employee''' class and remove the '''final '''keyword.
  
'''About Spoken Tutorial Project'''
+
Save the class.
  
 +
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the''' TestEmployee''' class
  
 +
Uncomment the lines '''manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); '''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Come to the''' TestEmployee''' class.
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link
+
Uncomment the lines '''manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts"); '''
  
* It summarizes the Spoken Tutorial project
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Save''' the class and '''run''' the program.
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Save''' the class and '''run''' the program.
  
* If you do not have good bandwidth, you can download and watch it
+
|-
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Highlight the output
  
 +
'''Name: Sneha'''
  
 +
'''Email: abc@gmail.com'''
 +
 +
'''Manager of: Accounts'''
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We got the desired output.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 10 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 10
  
'''About Spoken Tutorial workshops'''  
+
'''Summary'''  
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now let's summarize.
  
 +
In this tutorial we learnt:
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team
+
* When to invoke final keyword.
 +
* What are:  
 +
** '''final variables,'''
 +
** '''final methods '''and
 +
** '''final classes'''
  
* Conducts workshops using spoken tutorials
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 11
  
* Gives certificates for those who pass an online test
+
'''Assignment'''
 
+
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment,
* For more details, please write to contact at spoken hyphen tutorial dot org
+
  
 +
Repeat the steps of '''Using final keyword '''tutorial for the '''Bike''' and '''Vehicle class.''' which we used in the previous tutorial.
  
 +
Write down the '''classes''' in '''Java''' that are '''final classes. '''
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 11 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 12
  
'''Acknowledgement'''  
+
'''About Spoken Tutorial Project'''  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link.  It summarizes the Spoken Tutorial project
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 13
  
 +
'''About Spoken Tutorial workshops'''
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
+
| style="border:1pt solid #000000;padding:0.097cm;"| The Spoken Tutorial Project Team
  
* It is supported by the National Mission on Education through ICT, MHRD, Government of India
+
* Conducts workshops
 +
* Gives certificates for those who pass an online test
 +
* For more details, please write to us
  
* More information on this Mission is available at
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 14
  
* [http://spoken-tutorial.org/NMEICT- http://spoken-tutorial.org/NMEICT-] Intro
+
'''Acknowledgement'''
  
This is Trupti Kini''' '''from '''IIT Bombay '''signing off.
 
  
Thanks for joining.  
+
| style="border:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is supported by the National Mission on Education through ICT, MHRD, Government of India.
  
 +
That's it for this tutorial.
  
 +
This is Trupti Kini from '''IIT Bombay'''.
  
 +
Thank you very much for watching.
  
 
|}
 
|}

Latest revision as of 14:54, 24 April 2015

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.

We will also learn about -

  • final variables,
  • final methods,
  • final classes
Slide 3

Software Requirements


To record this tutorial, I am using
  • Ubuntu Linux version 12.04
  • JDK 1.7
  • Eclipse 4.3.1
Slide 4

Pre-requisites


To follow this tutorial, you must have,
  • Basic knowledge of Java and Eclipse IDE.
  • and 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.

final is a keyword or reserved word in Java.

It can be applied to variables, methods or classes.

What is final variable?

Slide 6

Now, let us learn what final variable is.

final variable is a variable whose value cannot be changed.

That is, it will be a constant.

Go to the Eclipse IDE

Point to MyProject

I am switching to Eclipse IDE now.

I had already created a project named MyProject in the previous tutorial.

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 name.
Type final keyword

Highlight variable name

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

The final field Employee.name cannot be assigned

We get compilation error.

The final field Employee.name cannot be assigned

Highlight

public final string name = “sneha”

This is because here the final variable name is already declared and initialised.

We can initialize a final variable only once.

Comment the method setName So we will comment the method setName which modifies the variable name.
Save the class. Save the class.
Go to TestEmployee class->main method

Type // before manager.setName("Nikkita Dinesh");

Now, come to TestEmployee class.

Come to the main method and comment the line manager.setName("Nikkita Dinesh");

We commented this line as it was instance of method setName.

We have already commented setName method in Employee class.


Save and Run the program. Now let us 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

Highlight the initialization in TestEmployee class and

Employee class.

We got this output as we already initialized variables with these values in

TestEmployee class and Employee class.

Go to Employee class->final variable name 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

The final field Employee.name cannot be assigned

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 have already learnt what a constructor is earlier.

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

and inside the curly brackets, lets initialize the variable name with value sneha semicolon .

Point to the method setName

Comment the method

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

The final field Employee.name cannot be assigned

We get error.

The final field Employee.name cannot be assigned

Slide 7 This is because static final variables cannot be initialized in the constructor.

They must be assigned a value with their declaration.

Or they must be declared in a static block.

Slide 8 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.

Go to Eclipse IDE Switch back to Eclipse IDE.
Go to the Employee class ->Employee()

Delete Employee() and type static.

Highlight static block

So now we will create a static block.

For that, in the Employee class, come to the constructor. we created.

Here instead of Employee(), we will type static.

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.
Come to the method setEmail

Type newEmail="abc@gmail.com";

Now, come to the method setEmail.

Inside the method we will type,

newEmail is equal to abc@gmail.com semicolon

We have modified the final variable newEmail.

Save & Run the program. Once again we will Save & Run the program.
Highlight the error,

The final local variable newEmail cannot be assigned.

We get error.

The final local variable newEmail cannot be assigned.

This is because final variable as parameter to a method cannot be modified by that method.

So lets remove the variable modification.

Now we will learn about final method.
Come to the method getDetails in employee class. Come to the method getDetails in employee class.
Type final keyword before getDetails. Add final keyword before method getDetails.

We have made the method as final.

Save and Run the program. Save and Run the program.
Point to the compilation error,

class Manager overrides final method getDetails.()

We get error,

class Manager overrides final method getDetails.()

Highlight method getDetails() in Manager class. Come to the method getDetails() in Manager class.
Highlight method getDetails() in Manager class. This is because if you make any method as final, you cannot override it.

Manager class method getDetails overrides getDetails method in Employee class.

Slide 9 What if final method is private?

private methods are not inherited by the child class.

So we can add a method getDetails() in the child class.

You can try this as an assignment.

Go to Eclipse IDE Switch back to Eclipse IDE.
Go to Employee class >>

remove final keyword>>

Remove static keyword

In Employee class, remove final keyword before method getDetails.

Remove static keyword before final variable name.

Instead of static

Type,

Employee()

Now, we will learn whether constructor can me declared as final or not.

For that we will again create a constructor.

So instead of static we will type, Employee parentheses.

Type final before the constructor Add final keyword before the constructor.
Save and run the program. Save and run the program.
Highlight compilation error,

Illegal modifier for the constructor in type Employee

We get error

Illegal modifier for the constructor in type Employee

Remove the final keyword before Employee() This is because, constructor cannot be final since constructors cannot not be inherited.

We will remove the final keyword before the constructor.

Now, we will learn about final class.
Type final keyword before the class Employee. Add final keyword before the class Employee. to make it final.
Save and Run the program. Save and Run the program.
Highlight the compilation error,

The method setEmail is undefined for the type Manager

We get an error,

The method setEmail is undefined for the type Manager

Come to TestEmployee class and comment the lines.

manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

To know the actual error, lets come to TestEmployee class and comment the lines.

manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

Save the class and run the program Save the class and run the program
Highlight the compilation error,

The type Manager cannot subclass the final class Employee

The actual error is

The type manager cannot subclass the final class Employee.

Here Manager class extends Employee class.

Come back to Employee class and remove the final keyword So let's come back to Employee class and remove the final keyword.

Save the class.

Come to the TestEmployee class

Uncomment the lines manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

Come to the TestEmployee class.

Uncomment the lines manager.setEmail("abc@gmail.com"); manager.setDepartment("Accounts");

Save the class and run the program. Save the class and run the program.
Highlight the output

Name: Sneha

Email: abc@gmail.com

Manager of: Accounts

We got the desired output.
Slide 10

Summary

Now let's summarize.

In this tutorial we learnt:

  • When to invoke final keyword.
  • What are:
    • final variables,
    • final methods and
    • final classes
Slide 11

Assignment

As an assignment,

Repeat the steps of Using final keyword tutorial for the Bike and Vehicle class. which we used in the previous tutorial.

Write down the classes in Java that are final classes.

Slide 12

About Spoken Tutorial Project

Watch the video available at the following link. It summarizes the Spoken Tutorial project
Slide 13

About Spoken Tutorial workshops

The Spoken Tutorial Project Team
  • Conducts workshops
  • Gives certificates for those who pass an online test
  • For more details, please write to us
Slide 14

Acknowledgement


Spoken Tutorial Project is supported by the National Mission on Education through ICT, MHRD, Government of India.

That's it for this tutorial.

This is Trupti Kini from IIT Bombay.

Thank you very much for watching.

Contributors and Content Editors

Nancyvarkey, Trupti