Java/C3/Static-Blocks/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:01 Welcome to the Spoken Tutorial on Static blocks .
00:06 In this tutorial we will learn: What are static blocks
00:10 How to declare static blocks and How to use static blocks.
00:16 Here we are using :Ubuntu 14.04 JDK 1 .7 and Eclipse 4.3.1
00:26 To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
00:34 You should also have the knowledge of instance variables, static variables and static methods in Java.
00:43 If not, for relevant Java tutorials, please visit the link shown.
00:48 Let us learn about static blocks.
00:52 A Static block is mostly used for initializing values of static variables.
00:59 A static block is declared using the static keyword.
01:03 Static blocks are executed when the class is loaded in memory.
01:08 If there are static blocks in a program, they are invoked before constructors.
01:14 We cannot access instance variables inside a static block.
01:19 Now, we will switch to Eclipse and create a new project called StaticBlockDemo.
01:26 Inside this project, we will create the necessary classes to demonstrate the usage of Static Blocks.
01:33 Right click on src folder and click New-> Class.
01:38 Type the name of the class as StudentEnroll and press Enter.
01:44 Now type the following code to represent the StudentEnroll class.
01:49 Note that there are two static variables count and orgname.
01:54 Now click on Source -> and select Generate Constructor using Fields.
02:00 Delete the super keyword from the generated code.
02:04 We want a message to be printed whenever the constructor is invoked.
02:09 So inside this constructor, type the following code to print “Constructor invoked”.
02:15 Now we will add a method showData( ) to this class to print the values of the variables.
02:21 So type the following code.
02:23 Now we will add a static block to initialize the values of count and orgname.
02:29 Type the following code.
02:32 The variables orgname and count are static variables.
02:36 This block of code within the curly brackets prefixed by the static keyword is a static block.
02:42 This static block initializes the values for count and orgname as 100 and IITM respectively.
02:51 Inside this static block, type the following code to print “static block-1 is invoked”.
02:58 Now we will add one more class containing the main method.
03:03 So right click on the default package, click New-> Class and then type the name as Demo.
03:11 Inside this class we will have the main method.
03:15 Type main and then press Ctrl+space to generate the main method.
03:21 We will create an object of StudentEnroll class.
03:25 Type the following code to create an object s1.
03:29 Now let us invoke the showData method to print the values.
03:33 Type s1.showData() semicolon
03:38 Now let us run the Demo program.
03:41 We can observe that static block is invoked before the constructor.
03:46 The values of count and orgname are initialized as defined in the static block.
03:53 Now let us go back to the StudentEnroll class.
03:57 Let us see what happens if we initialise the value of id inside the static block.
04:03 Inside the static block type, id equals IT01 semicolon
04:10 We can see that an error comes up.
04:13 It indicates that an instance variable cannot be accessed inside a static block
04:19 Now let us comment this line and proceed further.
04:25 Let us go back to the slides
04:27 A class can contain multiple static blocks.
04:30 In such a case they are called only once in the order that they appear in the source code.
04:37 Switch back to Eclipse to verify it.
04:40 Let us include one more static block after the existing one.
04:45 Type the following code.
04:47 This static block initializes the values for count and orgname as 200 and IITB respectively.
04:57 Inside this static block type the following code.
05:01 Now let us run the Demo program again.
05:04 From the output, We can verify that the second static block is invoked after the first.
05:10 The values of the static variables count and orgname are updated by the second static block.
05:18 They are 200 and IITB respectively.
05:22 Let us summarize.
05:24 In this tutorial we have learnt : What is a static block

How to declare and define a static block and How static blocks are invoked and executed.

05:37 For the Assignment, This assignment is a continuation of the Static Methods assignment.
05:44 Make sure that you have completed the Static Methods assignment.
05:48 As before design the class CarService as specified in this slide.
05:54 Identify the instance variables and static variables.
05:58 Define a constructor to initialize the values for instance variables.
06:03 Define a static block to initialize the values of static variables.
06:08 Also create a Demo class containing the main method.
06:12 Inside the main method, create a few objects of CarService and invoke the show( ) method.
06:19 The video at the following link summarizes the Spoken Tutorial Project.
06:24 Please download and watch it.
06:27 The Spoken Tutorial Project Team Conducts workshops using spoken tutorials and Gives certificates on passing the online tests.
06:35 For more details, please write to us.
06:39 Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
06:46 More information on this Mission is available at the link shown.
06:50 This script has been contributed by: Dept. of Information Technology, Amal Jyothi College of Engineering.
06:58 This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Jyotisolanki