Java/C2/Non-static-block/English-timed

From Script | Spoken-Tutorial
Jump to: navigation, search
Time Narration
00:02 Welcome to the Spoken Tutorial on Non-static block in java.
00:06 In this tutorial, we will learn:
00:08 About non-static block
00:10 When a non-static block is executed?
00:13 Simple example of non-static block
00:16 And Why we need constructors?
00:18 Here we are using:

Ubuntu version 11.10 Java Development Environment jdk 1.6 And Eclipse IDE 3.7.0

00:26 To follow this tutorial, you must know
00:29 how to create a constructor in Java using Eclipse.
00:33 If not, for relevant tutorials, please visit our website which is as shown.

http://www.spoken-tutorial.org

00:38 Now we will see, what a non-static block is.
00:42 Any code written between two curly brackets is a non-static block.
00:46 We can see the syntax here.
00:51 When is a non-static block executed ?
00:54 A non-static block is executed for each object that is created.
00:58 It executes before the constructor's execution.
01:04 It can initialize instance member variables of the class.
01:08 Any other execution like calculation could also be given in the block.
01:14 Now, let us switch to Eclipse and try to use a non-static block.
01:22 I have already opened a class named NonStaticTest in Eclipse.
01:28 I also have a created a class named A .
01:33 Now inside class A, I will first create a variable of type int.
01:38 So, type: int a semicolon, then press Enter.
01:46 Within curly brackets, type: System dot out dot println within brackets and double quotes Non static block of an instance of Class A semicolon.
02:12 Then, type: System dot out dot println within brackets and double quotes The value of a is plus a semicolon.
02:32 Now we will declare a constructor.
02:35 So, type: public A opening and closing brackets open the curly brackets press Enter.
02:51 Then type: System dot out dot println within brackets and double quotes Constructing object of type A semicolon.
03:10 Then type: System dot out dot println within brackets and double quotes The value of a is plus a semicolon.
03:35 Now save this file.
03:44 Inside class NonStaticTest in Eclipse let us create an object of the class A.
03:53 So, type: A space a1 equal to new space A opening and closing brackets semicolon.
04:08 Next line, we will create one more object of class A.
04:12 So, type: A space a2 equal to new space A opening and closing brackets semicolon.
04:25 Now, Save and run the file. So, press Ctrl & S and Ctrl & F11 keys.
04:32 We get the output as follows:
04:35 As we can see when the first object is created, the non-static block is executed.
04:45 Non-static block of an instance of class A and the instance variable 'a' is initialized to 0.
04:53 Only after that the constructor is executed. Constructing object of type A.
05:02 And here, the instance variable is again initialized to 0.
05:07 Then again when the second object is created, the non-static block is executed.
05:16 This process is repeated.
05:20 We can have multiple non-static blocks in a class.
05:25 In this case they execute in the sequence in which they appear in the class.
05:30 We can try that now.
05:34 Include one more block after the first one in class A.
05:43 So, again type inside curly brackets:
05:47 System dot out dot println within brackets and double quotes Second Non static block of an instance of Class A semicolon.
06:08 Then type: System dot out dot println within brackets and double quotes The value of a is plus a semicolon .
06:30 Now, save this file , press Ctrl & S key simultaneously then press Ctrl & F11 to run the program.
06:44 We get the output as follows.
06:48 We see that after the first block is executed, the second is executed.
06:58 Only then the constructor is executed.
07:07 Now you might feel why do we need constructors.
07:10 The answer is we don't need the default constructor.
07:15 But the non-static block cannot be parameterized.
07:18 You cannot have objects taking values from out side.
07:22 So non-static block is not a substitute for constructor.
07:27 So, let us summarize.
07:29 In this tutorial we have learnt
07:32 about non-static block and how to use this block.
07:35 For self assessment: Create a class named B.
07:39 Create a non-static block and a constructor as shown in the tutorial.
07:44 Create an object of class B in the class NonStaticTest already created.
07:49 Check the output.
07:51 To know more about the Spoken Tutorial Project,
07:53 watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial.
07:56 It summarizes the Spoken Tutorial project.
08:00 If you do not have good bandwidth, you can download and watch it.
08:03 The Spoken Tutorial project team:
08:06 Conducts workshops using spoken tutorials.
08:08 Gives certificates to those who pass an online test.
08:12 For more details, please write to contact@spoken-tutorial.org
08:18 Spoken Tutorial Project is a part of the Talk to a Teacher project.
08:22 It is supported by the National Mission on Education through ICT, MHRD, Government of India.
08:28 More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro.
08:37 Thus we have come to the end of this tutorial.
08:40 This is Arya Ratish from IIT Bombay, signing off. Thanks for joining.

Contributors and Content Editors

Arya Ratish, Devisenan, Gaurav, PoojaMoolya, Sandhya.np14, Sneha