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

From Script | Spoken-Tutorial
Jump to: navigation, search
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: '''
+
'''Keywords: video tutorial, final keyword, final variable, final method, final class'''
  
  
  
{|border=1
+
{| style="border-spacing:0;"
! '''Visual cue'''  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Visual Cue'''
! '''Narration'''  
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Narration'''
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 1
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Welcome to the spoken-tutorial on''' Using final keyword.'''
  
 
|-
 
|-
| 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 2
| 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;"| 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 '''
 
  
'''Software Requirements'''
 
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 4
  
 +
'''Prerequisites'''
  
| 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
 
  
* '''Ubuntu''' '''Linux '''version 12.04
 
* '''JDK '''1.7
 
* '''Eclipse '''4.3.1
 
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| 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.
  
 
|-
 
|-
| 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.
  
  
 +
'''f'''keywords are case sensitive, it should be final.done'''inal''' is a '''keyword''' or '''reserved''' word in '''Java'''.
  
| 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,
+
It can be applied to '''variables, methods '''and''' classes.'''
  
Knowledge of basics of '''Java''' and '''Eclipse IDE.'''  
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''What is final variable?'''
  
Knowledge of '''Subclassing''' and '''Method overriding '''.
+
Slide 6
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us learn what '''final''' '''variable''' is.
  
If not, for relevant '''Java '''tutorials, please visit our website.
 
  
 +
'''final variable''' is a variable whose value cannot be changed.
  
 +
That is, it will be a '''constant'''.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the''' Eclipse IDE >> '''Click on '''MyProject'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us switch 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'''.
  
'''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 called''' name.'''
  
The '''final keyword''' in java is used to restrict the user.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' final keyword'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type''' 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;"| Let us 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;"| Now we will '''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;"| '''What is final variable?'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error
  
'''Slide 6'''
+
'''cannot assign a value to 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;"| Now, let us learn what is '''final '''variable?
+
  
If you make any variable as '''final''', you cannot change the value of '''final '''variable.
+
'''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| It gives a compilation error.
  
  
i.e It will be constant.
+
'''“Cannot assign value to final variable name in the method as name<nowiki>=</nowiki>newName.”'''
  
 
|-
 
|-
| 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;"|  
| 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.
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is because here the '''final''' '''variable''' is already declared and initialised.
  
  
|-
+
But where?
| 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'''.
+
...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.
  
 
|-
 
|-
| 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;"| 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;"| Come to the variable '''name.'''
+
 
 +
Point to the method''' setName'''
 +
 
 +
Enclose the method inside /* */
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will go back to our code.
 +
 
 +
In the '''Employee '''class, we will comment the '''method setName'''.
  
 
|-
 
|-
| 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: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;"| Type '''final '''keyword before the variable '''name'''.
+
 
 +
Type // before '''manager.setName("Nikkita Dinesh"); '''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to''' TestEmployee''' '''class'''.
 +
 
 +
 
 +
Go 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.
 +
 
 +
 
 +
 
  
 
|-
 
|-
| 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 & 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 output
| 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,
+
  
'''cannot assign a value to final variable name '''
+
'''Name: Sneha'''
  
'''name=newName; '''
+
'''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;"| '''Slide 7'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Great!!!
| 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,
+
  
'''final''' variable if declared and not initialised then,
+
We got the output as,
  
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;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is because we have initialized '''final '''variable only once.
  
 
|-
 
|-
| 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'''->'''variable''' name'''
| 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.'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee '''class.
  
We have already learnt what is a '''constructor.'''
+
Go to the '''final variable''' '''name.'''
  
 
|-
 
|-
| 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;"| 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;"| We know that '''constructor''' has the same name as class name.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us remove the initialization of '''final''' '''variable''' '''name.'''
 +
 
 +
So we will remove''' “sneha”.'''
  
 
|-
 
|-
| 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;"| Go 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;"| Type Employee,
+
 
 +
Uncomment the method.
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us uncomment the method''' setName'''.
  
Parentheses,
 
  
Open curly brackets,
 
  
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;"| And we will '''Save''' and '''Run''' the program once again.
  
 
|-
 
|-
| 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");'''
+
'''cannot assign a value to final variable name'''
 +
 
 +
'''name<nowiki>=</nowiki>newName<nowiki>;</nowiki>'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| It gives a compilation error.
 +
 
 +
 
 +
It says that we cannot assign a value to''' final''' variable '''name.'''
  
 
|-
 
|-
| 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.
 +
 
 +
Not 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 '''
 
  
'''name="Sneha"; '''
+
We have already learnt what a''' constructor '''is earlier'''.'''
  
 
|-
 
|-
| 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;"|  
| 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;"| We know that''' constructor''' has the same name as '''class''' name.
 
+
You can only initialize a '''final''' variable once.
+
  
 
|-
 
|-
| 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;"| '''Type,'''
| 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.
+
 
 +
'''Employee() {  name<nowiki>="</nowiki>Sneha";'''
 +
 
 +
''' }'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type
 +
 
 +
'''Employee, parentheses, open and close curly brackets'''
 +
 
 +
 
 +
Inside the curly brackets, we will initialize''' 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 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.'''
+
 
 +
Enclose the method inside''' '''/* */
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us 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;"| We will '''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 get the output as,
  
Manager of: Accounts  
+
'''Name: Sneha'''
 +
 
 +
'''Email: abc@gmail.com'''
 +
 
 +
'''Manager of: Accounts'''
 +
 
 +
 
 +
'''<nowiki><<</nowiki>PAUSE>>'''
  
 
|-
 
|-
| 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-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;"| For that let us go to the '''Employee class'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let us 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 variable name'''
+
| 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;"| Let us go to the variable '''name'''.
+
| style="border:1pt solid #000000;padding:0.097cm;"| For that let us go back to the''' Employee class'''.
  
Let us add keyword '''static''' before '''final '''keyword.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the variable''' name'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us go to the variable''' name'''.
  
 
|-
 
|-
| 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;"| 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;"| Save & run the program.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us add the '''keyword static''' before''' final''' '''keyword''' to make it static.
  
 
|-
 
|-
| 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;"| '''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;"| We get a compilation error,
+
| style="border:1pt solid #000000;padding:0.097cm;"| And we will once again '''Save '''and '''Run''' the program.
  
'''cannot assign a value to final variable name '''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error
  
'''name="Sneha";'''
+
'''cannot assign a value to final variable name'''
 +
 
 +
'''name<nowiki>="</nowiki>Sneha";'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get a compilation error.
 +
 
 +
'''“Cannot assign a value to static final variable name.”'''
  
 
|-
 
|-
| 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;"| slides and document is not synchronized from this point and onward.  
| 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,
+
  
'''static final''' variables cannot be assigned value in '''constructor;''' they must be assigned a value with their declaration OR
+
<nowiki>Further slide 9.1 and 9.2 [i.e. 25,26] contains one extra word block in second bullet,</nowiki>doneSlide 7
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is because '''static final''' '''variables '''cannot be initialized in''' '''the''' constructor.'''
  
they must be declared ina '''static block'''.
+
 
 +
They must be assigned a value with their declaration.
 +
 
 +
 
 +
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 Is this bold text appropriate for the word "final"?Yes we need to, because the "final" word should not be translated.I asked this because I myself made that text bold. It was not so before.Ok may be I missed out to bold'''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;"| Let us 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 ->constructor
| 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,
 
  
'''Name: Sneha '''
+
Delete '''Employee '''and type '''static'''.
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let’s create a''' static block.'''
  
'''Email: abc@gmail.com '''
 
  
'''Manager of: Accounts '''
+
For that, in the '''Employee '''class, go to the '''constructor. '''
  
  
 +
Here instead of '''Employee(), '''type '''static'''.
  
 +
|-
 +
| 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;"|  
+
| 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;"| Now lets use '''final '''variable as parameter to the method.
+
 
 +
'''Name: Sneha'''
 +
 
 +
'''Email: abc@gmail.com'''
 +
 
 +
'''Manager of: Accounts'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will get 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;"| '''Point to class Employee'''
+
| 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;"| Let us go to the class '''Employee'''
+
| 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 the method setEmail'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Go to class''' Employee'''
| 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;"| Let us go to the '''Employee class.'''
  
Add''' final''' keyword before '''String newEmail'''.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the method''' setEmail'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us go to the method''' setEmail.'''
  
Save and run the program.
+
|-
 +
| 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''' to make the parameter final.
  
 
|-
 
|-
| 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;"| '''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;"| We will get the output,
+
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
  
'''Name: Sneha '''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the output
  
'''Email: abc@gmail.com '''
+
'''Name: Sneha'''
  
'''Manager of: Accounts '''
+
'''Email: abc@gmail.com'''
 +
 
 +
'''Manager of: Accounts'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will get 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;"| '''Type newEmail="kinitrupti@gmail.com";'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Back 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;"| Now lets type inside the method,
+
  
'''newEmail="kinitrupti@gmail.com";'''
+
Go to '''Employee '''class
  
Save & run the prorgam.
+
Go to the method''' setEmail'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to the '''Employee '''class.
 +
 
 +
Go to the '''method''' '''setEmail.'''
  
 
|-
 
|-
| 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;"| Type '''newEmail<nowiki>="</nowiki>kinitrupti@gmail.com";'''
| 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,
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now, inside the method lets initialize '''newEmail '''variable.
 +
 
 +
Inside the '''method''' type,
 +
 
 +
'''newEmail<nowiki>="</nowiki>abc'''It is not recommended to give out personal email-ids in demos.
 +
 
 +
 
 +
Remember 10,000 students will watch this tutorial.
 +
 
 +
 
 +
Pls create an id for demo purpose
 +
 
 +
OR
 +
 
 +
Use the spokentutorial official email-id.Done'''@gmail.com";'''
 +
 
 +
 
  
'''final parameter newEmail may not be assigned'''
 
  
 
|-
 
|-
| 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 '''Save''' & '''Run '''the program.
  
Value of '''final '''variable as parameter to a method cannot be changed by that method.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Highlight the error,
 +
 
 +
'''final parameter newEmail may not be assigned'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We will get a compilation error.
 +
 
 +
'''“final parameter newEmail may not 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;"| Now lets learn about '''final''' method.
+
| style="border:1pt solid #000000;padding:0.097cm;"| This is because '''final''' variable as parameter to a '''method '''cannot be modified by that '''method.'''
 +
 
 +
 
 +
'''<nowiki><<</nowiki>PAUSE>>'''
  
 
|-
 
|-
| 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;"|  
| 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.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Now let’s learn about''' final''' '''method'''.
  
 
|-
 
|-
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''Slide 12'''
+
| 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;"| If you make any method as final, you cannot '''override''' it.
+
 
 +
Go to the method''' setEmail'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Come back to the '''Employee class'''.
 +
 
 +
Go to the '''method''' '''setEmail.'''
  
 
|-
 
|-
| 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;"| Point to the''' newEmail '''initialization
| 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.
+
 
 +
remove '''newEmail<nowiki>="</nowiki>kinitrupti@gmail.com";'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let’s remove the''' newEmail''' initialization.
  
 
|-
 
|-
| 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:1pt solid #000000;padding:0.097cm;"| Go 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:1pt solid #000000;padding:0.097cm;"| Go 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;"| Type final keyword before''' getDetails.'''
| 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;"| Add''' final''' '''keyword''' before''' getDetails '''to make it final.
  
 
|-
 
|-
| 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 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;"| And as before, '''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;"| '''What if final method is private?'''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Point to the compilation error,
  
'''Slide 13'''
+
'''Cannot override the final method from Employee'''
| 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'''?
+
| style="border:1pt solid #000000;padding:0.097cm;"| We get compilation error,
  
Since '''private''' methods are inaccessible, they are implicitly final in Java.
+
'''“Cannot override the final method from Employee.”'''
  
So adding '''''final''' ''specifier to a '''private''' method doesn’t add any value.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is because 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 Employee class'''
+
| 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;"| Let us try this out.
+
| style="border:1pt solid #000000;padding:0.097cm;"| What if '''final method''' is''' private'''?
  
Come to the '''Employee''' class.
 
  
Let us create a constructor again as '''Employee.'''
+
'''private''' '''methods''' are not inherited by the '''child class.'''
  
Add final before the constructor.
+
 
 +
So we can add a '''method''' '''getDetails() '''in the '''child class'''.
 +
 
 +
 
 +
You can try this as an assignment.
  
 
|-
 
|-
| 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: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;"| Save and run the program.
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us switch back to '''Eclipse IDE.'''
  
We get a compilation error,
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go to '''Employee''' class
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now, let us see whether '''constructor''' can be declared as '''final '''or not.
  
'''modifier final not allowed here'''
+
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;"| '''Slide 14'''
+
| 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,
+
  
'''constructor''' cannot be '''final''' since constructors are not inherited.
+
'''Employee() {  name<nowiki>="</nowiki>Sneha";'''
  
 +
''' }'''
  
  
  
|-
+
| style="border:1pt solid #000000;padding:0.097cm;"| Let us create a '''constructor''' again as''' Employee.'''
| 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;"| Now lets learn about '''final class.'''
+
  
 +
Type
  
 +
'''Employee, parentheses, open and close curly brackets'''
  
 +
 +
Inside the brackets, 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 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;"| Remove static keyword.
  
Add '''final '''keyword before the class name.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Type '''final '''before the constructor
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Add '''final '''before the '''constructor'''.
  
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;"| Highlight compilation error,
  
 +
'''modifier final not allowed here'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get a compilation error once again.
  
 +
'''“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;"|  
+
| 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;"| 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;"| This is because '''constructor''' cannot be''' final''' since '''constructors''' are not inherited.
  
 
|-
 
|-
| 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;"|  
| 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;"| Next, let us learn about''' final class.'''
  
If you make any class as final, you cannot extend it.
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Go 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:none;padding:0.097cm;"| '''Slide 7 '''
+
| 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.'''
  
'''Summary'''  
+
|-
 +
| 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;"| Highlight the compilation error,
  
 +
'''The type Manager cannot subclass the final class Employee'''
 +
| style="border:1pt solid #000000;padding:0.097cm;"| We get the compilation error again. The error msg says-
  
| 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:
+
'''“The type Manager cannot subclass the final class Employee.”'''
  
* When to invoke final keyword.
 
* '''final''' keyword in various contexts:
 
** '''variable, '''
 
** '''method, '''
 
** '''class'''
 
  
  
  
 
|-
 
|-
| 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;"| Slide 10
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This is because if you make any '''class''' as '''final''', you cannot '''extend''' it.
  
'''Assignment'''  
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the keyword '''final '''before '''Employee '''class
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Remove the '''keyword''' '''final '''before '''Employee class'''.
  
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| '''Save''' and '''Run''' the program.
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Now '''Save''' and '''Run''' the program.
  
 +
|-
 +
| 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;"| Assignment
 
  
* Repeat the same as shown in the tutorial for the '''Bike '''and''' Vehicle class.'''<br/>
 
  
 +
| style="border:1pt solid #000000;padding:0.097cm;"| This time we get the output,
  
 +
'''Name: Sneha'''
  
 +
'''Email: abc@gmail.com'''
 +
 +
'''Manager of: Accounts'''
 +
 +
 +
'''<nowiki><<</nowiki>PAUSE>>'''
  
 
|-
 
|-
| 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;"|
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Let us summarize.
  
'''About Spoken Tutorial Project'''
+
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 11
  
 +
'''Summary'''
  
  
| 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
 
  
* It summarizes the Spoken Tutorial project
+
| style="border:1pt solid #000000;padding:0.097cm;"| In this tutorial we have learnt:
  
* If you do not have good bandwidth, you can download and watch it
+
* When to invoke final keyword.
 +
* What are:
 +
** '''final variables,'''
 +
** '''final methods '''and
 +
** '''final classes'''
  
  
  
 
|-
 
|-
| 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 12
  
'''About Spoken Tutorial workshops'''  
+
'''Assignment'''
  
  
  
| 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
+
| style="border:1pt solid #000000;padding:0.097cm;"| As an assignment
  
* Conducts workshops using spoken tutorials
+
*  
 +
** Are you sure about the name of the tutorial? Pls chkThe bike vehicle was given as an assignment in the previous tutorial.<br/> We need to frame it properly.Pls frame it properly.doneRepeat the steps of''' Using final keyword '''tutorial for the''' Bike''' and''' Vehicle class.'''
 +
** Write down the classes in java that are '''final''' classes.
  
* Gives certificates for those who pass an online test
 
  
* For more details, please write to contact at spoken hyphen tutorial dot org  
+
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 13
 +
 
 +
'''About Spoken Tutorial Project'''
 +
 
 +
 
 +
 
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Watch the video available at the following link '''http://spoken-tutorial.org/What is a Spoken Tutorial'''
 +
 
 +
* It summarizes the Spoken Tutorial project
 +
 
 +
* 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:none;padding:0.097cm;"| '''Slide 11 '''
+
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 14
  
'''Acknowledgement'''  
+
'''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
 +
 
 +
* Conducts workshops using spoken tutorials
 +
 
 +
* Gives certificates for those who pass an online test
 +
 
 +
* For more details, please write to contact at spoken hyphen tutorial dot org
 +
 
 +
 
 +
 
 +
|-
 +
| style="border:1pt solid #000000;padding:0.097cm;"| Slide 15
 +
 
 +
'''Acknowledgement'''
  
* It is supported by the National Mission on Education through ICT, MHRD, Government of India
 
  
* More information on this Mission is available at
 
  
* [http://spoken-tutorial.org/NMEICT- http://spoken-tutorial.org/NMEICT-] Intro
+
| style="border:1pt solid #000000;padding:0.097cm;"| Spoken Tutorial Project is a part of the Talk to a Teacher project
  
This is Trupti Kini''' '''from '''IIT Bombay '''signing off.
+
* It is supported by the National Mission on Education through ICT, MHRD, Government of India
  
Thanks for joining.
+
* More information on this Mission is available at
  
 +
* [http://spoken-tutorial.org/NMEICT- http][http://spoken-tutorial.org/NMEICT- ://][http://spoken-tutorial.org/NMEICT- spoken][http://spoken-tutorial.org/NMEICT- -][http://spoken-tutorial.org/NMEICT- tutorial][http://spoken-tutorial.org/NMEICT- .][http://spoken-tutorial.org/NMEICT- org][http://spoken-tutorial.org/NMEICT- /][http://spoken-tutorial.org/NMEICT- NMEICT][http://spoken-tutorial.org/NMEICT- -] Intro
  
 +
This is Trupti Kini from''' IIT Bombay''' signing off.
  
 +
Thanks for joining.
  
 
|}
 
|}

Revision as of 11:40, 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.


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

Prerequisites


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.


fkeywords are case sensitive, it should be final.doneinal 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.


final variable is a variable whose value cannot be changed.

That is, it will be a constant.

Go to the Eclipse IDE >> Click on MyProject Let us switch 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.
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.


“Cannot assign value to final variable name in the method as name=newName.”

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


But where?

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


Go 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. 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.


It says that we cannot assign a value to final variable name.

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

We know that constructor has the same name as class name.
Type,

Employee() { name="Sneha";

}

Type

Employee, parentheses, open and close curly brackets


Inside the curly brackets, we will initialize name as Sneha.

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


<<PAUSE>>

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.


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 Is this bold text appropriate for the word "final"?Yes we need to, because the "final" word should not be translated.I asked this because I myself made that text bold. It was not so before.Ok may be I missed out to boldfinal.

Go to Eclipse IDE Let us switch back to Eclipse IDE.
Go to the Employee class ->constructor


Delete Employee and type static.

Let’s create a static block.


For that, in the Employee class, go to the constructor.


Here instead of Employee(), type static.

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.


Remember 10,000 students will watch this tutorial.


Pls create an id for demo purpose

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.


<<PAUSE>>

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?


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 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


Inside the brackets, we will declare name as Sneha.

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


<<PAUSE>>

Let us summarize.
Slide 11

Summary


In this tutorial we have learnt:
  • When to invoke final keyword.
  • What are:
    • final variables,
    • final methods and
    • final classes


Slide 12

Assignment


As an assignment
    • Are you sure about the name of the tutorial? Pls chkThe bike vehicle was given as an assignment in the previous tutorial.
      We need to frame it properly.Pls frame it properly.doneRepeat the steps of Using final keyword tutorial for the Bike and Vehicle class.
    • Write down the classes in java that are final classes.


Slide 13

About Spoken Tutorial Project


Watch the video available at the following link http://spoken-tutorial.org/What is a Spoken Tutorial
  • It summarizes the Spoken Tutorial project
  • If you do not have good bandwidth, you can download and watch it


Slide 14

About Spoken Tutorial workshops


The Spoken Tutorial Project Team
  • Conducts workshops using spoken tutorials
  • Gives certificates for those who pass an online test
  • For more details, please write to contact at spoken hyphen tutorial dot org


Slide 15

Acknowledgement


Spoken Tutorial Project is a part of the Talk to a Teacher project
  • It is supported by the National Mission on Education through ICT, MHRD, Government of India
  • More information on this Mission is available at

This is Trupti Kini from IIT Bombay signing off.

Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Trupti