Difference between revisions of "Java/C2/Non-static-block/English-timed"
From Script | Spoken-Tutorial
PoojaMoolya (Talk | contribs) |
|||
(10 intermediate revisions by 3 users not shown) | |||
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: |
− | + | ||
|- | |- | ||
| 00:08 | | 00:08 | ||
− | | About '''non-static''' '''block''' | + | |About '''non-static''' '''block''' |
|- | |- | ||
| 00:10 | | 00:10 | ||
− | | | + | |When a '''non-static block''' is executed? |
|- | |- | ||
| 00:13 | | 00:13 | ||
− | | | + | |Simple example of '''non-static block''' |
|- | |- | ||
| 00:16 | | 00:16 | ||
|And Why we need '''constructors'''? | |And Why we need '''constructors'''? | ||
− | |||
− | |||
|- | |- | ||
| 00:18 | | 00:18 | ||
− | | Here we are using | + | | Here we are using: |
− | + | '''Ubuntu version 11.10''' | |
− | + | '''Java Development Environment jdk 1.6''' | |
− | + | And '''Eclipse IDE 3.7.0''' | |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 00:26 | | 00:26 | ||
− | |To follow this tutorial you must know | + | |To follow this tutorial, you must know |
|- | |- | ||
| 00:29 | | 00:29 | ||
− | | | + | |how to create a '''constructor''' in '''Java''' using '''Eclipse'''. |
|- | |- | ||
| 00:33 | | 00:33 | ||
− | | If not, for relevant tutorials please visit our website which is as shown | + | | If not, for relevant tutorials, please visit our website which is as shown. |
− | + | http://www.spoken-tutorial.org | |
− | + | ||
|- | |- | ||
| 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 73: | Line 64: | ||
|- | |- | ||
| 00:54 | | 00:54 | ||
− | |A '''non-static block''' is | + | |A '''non-static block''' is executed for each object that is created. |
|- | |- | ||
| 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 96: | Line 84: | ||
|- | |- | ||
| 01:22 | | 01:22 | ||
− | | I have already opened a class named '''NonStaticTest''' in '''Eclipse.''' | + | | I have already opened a '''class''' named '''NonStaticTest''' in '''Eclipse.''' |
− | + | ||
|- | |- | ||
| 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'''. |
− | + | ||
|- | |- | ||
| 01:38 | | 01:38 | ||
− | | So type '''int''' | + | | So, type: '''int a''' semicolon, then press '''Enter'''. |
− | + | ||
|- | |- | ||
| 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 130: | Line 112: | ||
|- | |- | ||
| 02:35 | | 02:35 | ||
− | | So type '''public A | + | | 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. |
− | + | ||
|- | |- | ||
| 04:08 | | 04:08 | ||
− | | Next line we will create one more object of class '''A'''. | + | | Next line, we will create one more object of class '''A'''. |
|- | |- | ||
| 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 153: | ||
| 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''' 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 177: | ||
| 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. | ||
− | |||
|- | |- | ||
| 05:34 | | 05:34 | ||
− | | Include one more block after the first one in class A. | + | | Include one more block after the first one in class '''A'''. |
|- | |- | ||
| 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''' semicolon. |
− | + | ||
|- | |- | ||
| 06:08 | | 06:08 | ||
− | | Then type '''System | + | | Then type: '''System ''' dot '''out''' dot '''println''' within brackets and double quotes '''The value of a is''' plus '''a ''' semicolon . |
− | + | ||
|- | |- | ||
| 06:30 | | 06:30 | ||
− | | Now save this file ,press''' Ctrl & S | + | | Now, save this file , press''' Ctrl & S''' key simultaneously then press ''' Ctrl & F11 ''' to '''run''' the program. |
|- | |- | ||
Line 247: | Line 212: | ||
|- | |- | ||
| 06:48 | | 06:48 | ||
− | | | + | | We see that after the first block is executed, the second is executed. |
− | + | ||
|- | |- | ||
Line 257: | Line 221: | ||
| 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.''' |
|- | |- | ||
| 07:27 | | 07:27 | ||
− | | So let us | + | | So, let us summarize. |
|- | |- | ||
| 07:29 | | 07:29 | ||
− | | In this tutorial we learnt | + | | In this tutorial we have learnt |
− | + | ||
|- | |- | ||
| 07:32 | | 07:32 | ||
− | | | + | | about '''non-static block''' and how to use this block. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| 07:35 | | 07:35 | ||
− | | For self assessment | + | | For self assessment: Create a '''class '''named '''B.''' |
− | |||
− | |||
− | |||
|- | |- | ||
| 07:39 | | 07:39 | ||
− | | | + | |Create a '''non-static block''' and a '''constructor''' as shown in the tutorial. |
|- | |- | ||
| 07:44 | | 07:44 | ||
− | | | + | |Create an '''object''' of '''class B''' in the '''class NonStaticTest '''already created. |
|- | |- | ||
| 07:49 | | 07:49 | ||
− | | | + | |Check the output. |
− | + | ||
− | + | ||
|- | |- | ||
| 07:51 | | 07:51 | ||
− | | To know more about the Spoken Tutorial Project | + | | To know more about the Spoken Tutorial Project, |
|- | |- | ||
| 07:53 | | 07:53 | ||
− | | | + | | watch the video available at http://spoken-tutorial.org/What_is_a_Spoken_Tutorial. |
|- | |- | ||
| 07:56 | | 07:56 | ||
− | | It summarizes the Spoken Tutorial project | + | | It summarizes the Spoken Tutorial project. |
|- | |- | ||
| 08:00 | | 08:00 | ||
− | | If you do not have good bandwidth, you can download and watch it | + | | If you do not have good bandwidth, you can download and watch it. |
− | + | ||
− | + | ||
|- | |- | ||
| 08:03 | | 08:03 | ||
− | | The Spoken Tutorial | + | | The Spoken Tutorial project team: |
|- | |- | ||
| 08:06 | | 08:06 | ||
− | | Conducts workshops using spoken tutorials | + | | Conducts workshops using spoken tutorials. |
|- | |- | ||
| 08:08 | | 08:08 | ||
− | | Gives certificates | + | | Gives certificates to those who pass an online test. |
|- | |- | ||
| 08:12 | | 08:12 | ||
| For more details, please write to contact@spoken-tutorial.org | | For more details, please write to contact@spoken-tutorial.org | ||
− | |||
− | |||
|- | |- | ||
| 08:18 | | 08:18 | ||
− | | Spoken Tutorial Project is a part of the Talk to a Teacher project | + | | Spoken Tutorial Project is a part of the Talk to a Teacher project. |
|- | |- | ||
| 08:22 | | 08:22 | ||
− | | | + | | It is supported by the National Mission on Education through ICT, MHRD, Government of India. |
|- | |- | ||
| 08:28 | | 08:28 | ||
− | | More information on this | + | | More information on this mission is available at http://spoken-tutorial.org/NMEICT-Intro. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 372: | Line 315: | ||
|- | |- | ||
| 08:40 | | 08:40 | ||
− | | This is Arya Ratish | + | | This is Arya Ratish from IIT Bombay, signing off. Thanks for joining. |
− | + | ||
|} | |} |
Latest revision as of 15:30, 28 March 2017
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. |
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