Difference between revisions of "Java/C2/Non-static-block/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
Line 4: | Line 4: | ||
|- | |- | ||
| 00:02 | | 00:02 | ||
− | | Welcome to the Spoken Tutorial on '''Non-static block '''in '''java'''. | + | | Welcome to the Spoken Tutorial on '''Non-static block''' in '''java'''. |
|- | |- | ||
| 00:06 | | 00:06 | ||
| In this tutorial we will learn | | In this tutorial we will learn | ||
− | |||
|- | |- | ||
Line 17: | Line 16: | ||
|- | |- | ||
| 00:10 | | 00:10 | ||
− | | When a '''non-static block '''executed? | + | | When a '''non-static block''' executed? |
|- | |- | ||
Line 26: | Line 25: | ||
| 00:16 | | 00:16 | ||
|And Why we need '''constructors'''? | |And Why we need '''constructors'''? | ||
− | |||
− | |||
|- | |- | ||
Line 36: | Line 33: | ||
* Java Development Environment jdk 1.6 | * Java Development Environment jdk 1.6 | ||
*And Eclipse IDE 3.7.0 | *And Eclipse IDE 3.7.0 | ||
− | |||
− | |||
|- | |- | ||
Line 56: | Line 51: | ||
| 00:38 | | 00:38 | ||
| Now we will see what a '''non-static block''' is. | | Now we will see what a '''non-static block''' is. | ||
− | |||
|- | |- | ||
| 00:42 | | 00:42 | ||
| Any code written between two curly brackets is a '''non-static block'''. | | Any code written between two curly brackets is a '''non-static block'''. | ||
− | |||
|- | |- | ||
Line 78: | Line 71: | ||
| 00:58 | | 00:58 | ||
| It executes before the '''constructor''''s execution. | | It executes before the '''constructor''''s execution. | ||
− | |||
|- | |- | ||
| 01:04 | | 01:04 | ||
| It can initialize''' instance member variables '''of the class. | | It can initialize''' instance member variables '''of the class. | ||
− | |||
|- | |- | ||
| 01:08 | | 01:08 | ||
| Any other execution like calculation could also be given in the block. | | Any other execution like calculation could also be given in the block. | ||
− | |||
|- | |- | ||
Line 101: | Line 91: | ||
|- | |- | ||
| 01:28 | | 01:28 | ||
− | | I also have a created a '''class '''named '''A''' . | + | | I also have a created a '''class''' named '''A''' . |
|- | |- | ||
| 01:33 | | 01:33 | ||
− | | Now inside '''class''' '''A ''', I will first create a variable of type '''int'''. | + | | Now inside '''class''' '''A''', I will first create a variable of type '''int'''. |
Line 116: | Line 106: | ||
|- | |- | ||
| 01:46 | | 01:46 | ||
− | | Within curly brackets type '''System | + | | 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 | | 02:12 | ||
− | | Then type '''System | + | | Then type '''System''' dot '''out''' dot '''println''' within brackets and double quotes '''The value of a''' is plus '''a''' semicolon. |
Line 131: | Line 120: | ||
| 02:35 | | 02:35 | ||
| So type '''public A''' ''opening and closing brackets'', open the ''curly brackets'' press '''Enter'''. | | So type '''public A''' ''opening and closing brackets'', open the ''curly brackets'' press '''Enter'''. | ||
− | |||
|- | |- | ||
| 02:51 | | 02:51 | ||
− | | Then type '''System | + | | Then type '''System''' dot '''out''' dot '''println''' within brackets and double quotes '''Constructing object of type A''' semicolon. |
− | + | ||
|- | |- | ||
| 03:10 | | 03:10 | ||
− | | Then type '''System | + | | Then type '''System''' dot '''out '''dot '''println''' within brackets and double quotes '''The value of a''' is ''plus'' '''a ''' semicolon. |
− | + | ||
|- | |- | ||
| 03:35 | | 03:35 | ||
| Now save this file. | | Now save this file. | ||
+ | |||
|- | |- | ||
| 03:44 | | 03:44 | ||
− | | Inside '''class NonStaticTest''' in''' Eclipse '''let us create an '''object '''of the '''class A'''. | + | | Inside '''class NonStaticTest''' in '''Eclipse''' let us create an '''object''' of the '''class A'''. |
− | + | ||
|- | |- | ||
| 03:53 | | 03:53 | ||
− | | So type '''A | + | | So type '''A''' space '''a1''' ''equal to'' ''' new''' space '''A''' opening and closing brackets semicolon |
− | + | ||
|- | |- | ||
Line 162: | Line 147: | ||
|- | |- | ||
| 04:12 | | 04:12 | ||
− | |So type '''A''' | + | |So type '''A''' space '''a2''' ''equal to'' '''new ''' space '''A''' opening and closing brackets semicolon. |
|- | |- | ||
| 04:25 | | 04:25 | ||
− | |Now Save and run the file. So press '''Ctrl &S''' and '''Ctrl &F11 '''keys | + | |Now Save and run the file. So press '''Ctrl &S''' and '''Ctrl &F11''' keys |
Line 177: | Line 162: | ||
| 04:35 | | 04:35 | ||
| As we can see when the first''' object '''is created, the '''non-static block''' is executed. | | As we can see when the first''' object '''is created, the '''non-static block''' is executed. | ||
− | |||
|- | |- | ||
| 04:45 | | 04:45 | ||
− | | '''non-static block''' of an''' instance of class A | + | | '''non-static block''' of an '''instance of class A''' and the''' instance variable 'a' '''is initialized to '''0'''. |
− | + | ||
|- | |- | ||
| 04:53 | | 04:53 | ||
| Only after that the '''constructor''' is executed. Constructing object of type A | | Only after that the '''constructor''' is executed. Constructing object of type A | ||
− | |||
|- | |- | ||
| 05:02 | | 05:02 | ||
| And here the''' instance variable''' is again initialized to '''0'''. | | And here the''' instance variable''' is again initialized to '''0'''. | ||
− | |||
|- | |- | ||
| 05:07 | | 05:07 | ||
| Then again when the second '''object''' is created, the '''non-static block''' is executed. | | Then again when the second '''object''' is created, the '''non-static block''' is executed. | ||
− | |||
|- | |- | ||
Line 206: | Line 186: | ||
| 05:20 | | 05:20 | ||
| We can have multiple '''non-static blocks''' in a '''class'''. | | We can have multiple '''non-static blocks''' in a '''class'''. | ||
− | |||
|- | |- | ||
| 05:25 | | 05:25 | ||
| In this case they execute in the sequence in which they appear in the '''class.''' | | In this case they execute in the sequence in which they appear in the '''class.''' | ||
− | |||
|- | |- | ||
| 05:30 | | 05:30 | ||
| We can try that now. | | We can try that now. | ||
− | |||
|- | |- | ||
Line 225: | Line 202: | ||
| 05:43 | | 05:43 | ||
| So again type inside curly brackets. | | So again type inside curly brackets. | ||
− | |||
|- | |- | ||
| 05:47 | | 05:47 | ||
− | | '''System''' ''dot '''''out''' ''dot'' '''println''' within brackets and double quotes '''Second''' '''Non static''' '''block of an instance of Class A | + | | '''System''' ''dot '''''out''' ''dot'' '''println''' within brackets and double quotes '''Second''' '''Non static''' '''block of an instance of Class A '''semicolon. |
− | + | ||
|- | |- | ||
| 06:08 | | 06:08 | ||
| Then type '''System '''''dot'' '''out''' ''dot '''''println''' within brackets and double quotes '''The value of a''' is ''plus '''''a '''''semicolon''. | | Then type '''System '''''dot'' '''out''' ''dot '''''println''' within brackets and double quotes '''The value of a''' is ''plus '''''a '''''semicolon''. | ||
− | |||
|- | |- | ||
Line 247: | Line 221: | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | | + | | we see that after the first block is executed, the second is executed. |
− | + | ||
|- | |- | ||
Line 257: | Line 230: | ||
| 07:07 | | 07:07 | ||
| Now you might feel why do we need '''constructors.''' | | Now you might feel why do we need '''constructors.''' | ||
− | |||
|- | |- | ||
| 07:10 | | 07:10 | ||
| The answer is we don't need the '''default constructor'''. | | The answer is we don't need the '''default constructor'''. | ||
− | |||
|- | |- | ||
| 07:15 | | 07:15 | ||
| But the '''non-static block''' cannot be '''parameterized'''. | | But the '''non-static block''' cannot be '''parameterized'''. | ||
− | |||
|- | |- | ||
| 07:18 | | 07:18 | ||
− | | You cannot have''' objects''' taking values from out side. | + | | You cannot have '''objects''' taking values from out side. |
− | + | ||
|- | |- | ||
| 07:22 | | 07:22 | ||
− | | So '''non-static block '''is not a substitute for '''constructor.''' | + | | So '''non-static block''' is not a substitute for '''constructor.''' |
|- | |- | ||
Line 285: | Line 254: | ||
| 07:29 | | 07:29 | ||
| In this tutorial we learnt : | | In this tutorial we learnt : | ||
− | |||
|- | |- | ||
| 07:32 | | 07:32 | ||
| About '''non-static block''' and how to use this block. | | About '''non-static block''' and how to use this block. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 301: | Line 266: | ||
| 07:36 | | 07:36 | ||
| create a '''class '''named '''B.''' | | create a '''class '''named '''B.''' | ||
+ | |||
|- | |- | ||
| 07:39 | | 07:39 | ||
Line 307: | Line 273: | ||
|- | |- | ||
| 07:44 | | 07:44 | ||
− | | Create an '''object''' of '''class B''' in the '''class NonStaticTest '''already created | + | | Create an '''object''' of '''class B''' in the '''class NonStaticTest '''already created. |
|- | |- | ||
| 07:49 | | 07:49 | ||
| Check the output. | | Check the output. | ||
− | |||
− | |||
|- | |- |
Revision as of 15:05, 23 September 2014
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 executed? |
00:13 | Simple example of non-static block |
00:16 | And Why we need constructors? |
00:18 | Here we are using
|
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 executedfor 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 summarizes |
07:29 | In this tutorial we learnt : |
07:32 | About non-static block and how to use this block. |
07:35 | For self assessment, |
07:36 | 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 for 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
|
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