From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the spoken-tutorial on Using final keyword.
00:05 In this tutorial, we will learn about the final keyword and when to invoke it.
00:11 We will also learn about - final variables , final methods , final classes.
00:18 To record this tutorial, I am using: Ubuntu Linux version 12.04 , JDK 1.7 , Eclipse 4.3.1
00:30 To follow this tutorial, you must have basic knowledge of Java and Eclipse IDE.
00:36 You must have knowledge of Subclassing and Method overriding.
00:41 If not, for relevant Java tutorials, please visit our website.
00:46 First of all, we will learn what final keyword is.
00:50 final is a keyword or reserved word in Java.
00:55 It can be applied to variables, methods or classes.
01:01 Now, let us learn what final variable is.
01:05 final variable is a variable whose value cannot be changed. That is, it will be a constant.
01:13 I am switching to Eclipse IDE now. I had already created a project named MyProject in the previous tutorial.
01:22 So, we will directly go to the Employee class of the project.
01:26 Come to the variable name.
01:30 Add final keyword before the variable name. We have made the variable name as final.
01:40 We will initialize the variable name with value "sneha".
01:45 We will Save and Run the program.
01:48 We get compilation error: The final field cannot be assigned
01:55 This is because here the final variable 'name' is already declared and initialized.
02:05 We can initialize a final variable only once.
02:08 So, we will comment the method setName which modifies the variable name.
02:14 Save the class.
02:16 Now, come to TestEmployee class.
02:19 Come to the main method and comment the line manager.setName("Nikkita Dinesh");
02:26 We commented this line as it was instance of method setName.
02:31 We have already commented setName method in Employee class.
02:35 Now let us Save the class and Run the program.
02:38 Great!!! We got the output, Name: Sneha ,Email: Manager of: Accounts .
02:47 We got this output as we already initialized variables with these values in TestEmployee class and Employee class.
02:58 Now come to the final variable name in Employee class.
03:02 Remove the initialization of final variable name. That is, remove “sneha”.
03:08 Uncomment the method setName.
03:12 Save and Run the program.
03:14 We get the error: The final field cannot be assigned
03:20 This is because, if the final variable is not initialized, then only constructor can initialize it.
03:28 That is, it cannot be modified anywhere else in the program.
03:33 For that, let’s create a constructor in the Employee class. We have already learnt what a constructor is earlier.
03:43 We know that constructor has the same name as class name.
03:47 So, we will type: Employee, parentheses, open and close curly brackets and inside the curly brackets, let's initialize the variable name with value sneha semicolon.
04:08 Comment the method setName.
04:12 Save and Run the program.
04:15 We got the desired output.
04:17 final variable is successfully initialized in a constructor.
04:22 Now we will learn about final static variables.
04:26 Come to the final variable in Employee class.
04:30 Add static keyword before final keyword. We have made the final variable as static.
04:38 Save and Run the program.
04:40 We get error: The final field cannot be assigned
04:46 This is because static final variables cannot be initialized in the constructor.
04:53 They must be assigned a value with their declaration. Or they must be declared in a static block.
05:01 static variables are shared among all the objects of a class.
05:06 Creating a new object would change the static variable. This is not allowed if the static variable is final.
05:14 Switch back to Eclipse IDE.
05:17 So, now we will create a static block.
05:20 For that, in the Employee class, come to the constructor we created.
05:26 Here, instead of Employee parenthesis , we will type static. We have created a static block.
05:35 Now we will Save and Run the program.
05:38 We got the desired output. We have successfully initialized static final variable.
05:46 Now let’s use final variable as a parameter to the method.
05:52 Come to the method setEmail in Employee class.
05:55 Add final keyword before String newEmail. We have made the parameter as final.
06:03 Save and Run the program.
06:06 We got the desired output.
06:09 Now, come to the method setEmail. Inside the method, we will type: newEmail is equal to semicolon
06:28 We have modified the final variable newEmail.
06:32 Once again we will Save & Run the program.
06:35 We get error: The final local variable newEmail cannot be assigned.
06:42 This is because final variable as parameter to a method cannot be modified by that method.
06:50 So, let's remove the variable modification.
06:54 Now we will learn about final method. Come to the method getDetails in employee class.
07:01 Add final keyword before method getDetails. We have made the method as final.
07:08 Save and Run the program.
07:10 We get error: class Manager overrides final method getDetails().
07:16 Come to the method getDetails() in Manager class.
07:21 This is because if you make any method as final, you cannot override it.
07:29 Manager class method getDetails overrides getDetails method in Employee class.
07:36 What if final method is private?
07:39 private methods are not inherited by the child class.
07:43 So, we can add a method getDetails() in the child class. You can try this as an assignment.
07:51 Switch back to Eclipse IDE.
07:54 In Employee class, remove final keyword before method getDetails.
08:03 Remove static keyword before final variable name.
08:10 Now, we will learn whether constructor can be declared as final or not.
08:15 For that, we will again create a constructor. So, instead of static we will type: Employee parentheses.
08:26 Add final keyword before the constructor.
08:31 Save and run the program.
08:36 We get error: Illegal modifier for the constructor in type Employee.
08:42 This is because, constructor cannot be final since constructors cannot be inherited.
08:50 We will remove the final keyword before the constructor.
08:54 Now, we will learn about final class.
08:57 Add final keyword before the class Employee to make it final.
09:03 Save and Run the program.
09:06 We get error: The method setEmail is undefined for the type Manager.
09:12 To know the actual error, let's come to TestEmployee class and comment the lines
09:21 manager.setEmail(""); manager.setDepartment("Accounts");
09:28 Save the class and run the program.
09:31 The actual error is The type manager cannot subclass the final class Employee.
09:40 Here, Manager class extends Employee class.
09:45 So, let's come back to Employee class and remove the final keyword. Save the class.
09:54 Come to the TestEmployee class. Uncomment the lines manager.setEmail(""); manager.setDepartment("Accounts");
10:06 Save the class and run the program.
10:09 We got the desired output.
10:12 Now let's summarize. In this tutorial, we learnt: When to invoke final keyword , What are final variables, final methods and final classes.
10:27 As an assignment, Repeat the steps of Using final keyword tutorial for the Bike and Vehicle class which we used in the previous tutorial.
10:37 Write down the classes in Java that are final classes.
10:41 Watch the video available at the following link. It summarizes the Spoken Tutorial project.
10:47 The Spoken Tutorial Project Team: Conducts workshops ,Gives certificates to those who pass an online test. For more details, please write to us.
10:56 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

Jyotisolanki, PoojaMoolya, Pratik kamble, Sandhya.np14