Difference between revisions of "Java/C3/Static-Blocks/English"
Nancyvarkey (Talk | contribs) |
|||
Line 23: | Line 23: | ||
* How to declare '''static blocks ''' | * How to declare '''static blocks ''' | ||
* How to use '''static blocks''' | * How to use '''static blocks''' | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| In this tutorial we will learn: | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| In this tutorial we will learn: | ||
Line 30: | Line 28: | ||
* How to declare '''static blocks '''and | * How to declare '''static blocks '''and | ||
* How to use''' static blocks ''' | * How to use''' static blocks ''' | ||
− | |||
− | |||
|- | |- | ||
Line 41: | Line 37: | ||
* '''JDK 1 .7''' | * '''JDK 1 .7''' | ||
* '''Eclipse 4.3.1''' | * '''Eclipse 4.3.1''' | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Here we are using | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Here we are using | ||
Line 48: | Line 42: | ||
* '''JDK 1 .7 '''and | * '''JDK 1 .7 '''and | ||
* '''Eclipse 4.3.1''' | * '''Eclipse 4.3.1''' | ||
− | |||
− | |||
|- | |- | ||
Line 59: | Line 51: | ||
* Knowledge of '''instance variables, static variables''' and''' static methods''' in''' Java''' | * Knowledge of '''instance variables, static variables''' and''' static methods''' in''' Java''' | ||
* For relevant tutorials, please visit http://www.spoken-tutorial.org | * For relevant tutorials, please visit http://www.spoken-tutorial.org | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| To follow this tutorial, you must have knowledge of basics of''' Java''' and''' Eclipse IDE.''' | ||
Line 74: | Line 64: | ||
'''Static Blocks''' | '''Static Blocks''' | ||
− | * A '''Static block '''is mostly used for | + | * A '''Static block '''is mostly used for initializing values of '''static variables''' |
− | + | * A '''static block''' is declared using the '''static''' keyword | |
− | * | + | * '''Static blocks''' are executed when the '''class''' is loaded in memory |
− | + | ||
− | ''' | + | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us learn about '''static blocks'''. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us learn about '''static blocks'''. | ||
− | |||
* A '''Static block '''is mostly used for initializing values of '''static variables. ''' | * A '''Static block '''is mostly used for initializing values of '''static variables. ''' | ||
* A '''static block''' is declared using the '''static''' keyword. | * A '''static block''' is declared using the '''static''' keyword. | ||
* '''Static blocks''' are executed when the '''class '''is loaded in memory. | * '''Static blocks''' are executed when the '''class '''is loaded in memory. | ||
− | |||
− | |||
|- | |- | ||
Line 101: | Line 82: | ||
* Static blocks are invoked before '''constructors ''' | * Static blocks are invoked before '''constructors ''' | ||
* Instance variables cannot be accessed''' '''inside''' static blocks ''' | * Instance variables cannot be accessed''' '''inside''' static blocks ''' | ||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| If there are '''static blocks''' in a program, they are invoked before '''constructors.''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| If there are '''static blocks''' in a program, they are invoked before '''constructors.''' | ||
Line 107: | Line 87: | ||
We cannot access '''instance variables''' inside a '''static block.''' | We cannot access '''instance variables''' inside a '''static block.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 119: | Line 96: | ||
− | Inside this project, we will create the necessary classes to demonstrate the usage of''' Static Blocks.''' | + | Inside this project, we will create the necessary '''classes''' to demonstrate the usage of''' Static Blocks.''' |
|- | |- | ||
Line 129: | Line 106: | ||
− | Type the name of the class as''' StudentEnroll''' and press '''Enter'''. | + | Type the name of the '''class''' as''' StudentEnroll''' and press '''Enter'''. |
|- | |- | ||
Line 140: | Line 117: | ||
private static String orgname; | private static String orgname; | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now type the following code to represent the''' StudentEnroll''' class. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now type the following code to represent the''' StudentEnroll''' class. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Highlight '''count''' and''' orgname''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Highlight '''count''' and''' orgname''' | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Note that there are two''' static variables count''' and''' orgname'''. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Note that there are two''' static variables count''' and''' orgname'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| click on''' Source ->''' and select''' Generate Constructor using Fields''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| click on''' Source ->''' and select''' Generate Constructor using Fields''' | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now click on''' Source ->''' and select''' Generate Constructor using Fields''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now click on''' Source ->''' and select''' Generate Constructor using Fields''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Delete the '''super''' keyword | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Delete the '''super''' keyword | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Delete the '''super''' keyword from the generated code. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Delete the '''super''' keyword from the generated code. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 169: | Line 134: | ||
System.''out''.println("constructor invoked"); | System.''out''.println("constructor invoked"); | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We want a message to be printed whenever the '''constructor''' is invoked. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We want a message to be printed whenever the '''constructor''' is invoked. | ||
Line 176: | Line 139: | ||
So inside this '''constructor''', type the following code to print “'''Constructor invoked'''”. | So inside this '''constructor''', type the following code to print “'''Constructor invoked'''”. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 190: | Line 150: | ||
} | } | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add a method''' showData'''( ) to this class to print the values of the variables. | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add a method''' showData'''( ) to this '''class''' to print the values of the variables. |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
+ | So type the following code. | ||
|- | |- | ||
Line 214: | Line 171: | ||
− | + | |- | |
+ | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Highlight '''orgname''' and '''count''' | ||
+ | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| The variables '''orgname''' and '''count''' are '''static variables'''. | ||
This block of code within the curly brackets prefixed by the '''static''' keyword is a '''static block'''. | This block of code within the curly brackets prefixed by the '''static''' keyword is a '''static block'''. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Highlight''' count''' and''' orgname''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Highlight''' count''' and''' orgname''' | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| This '''static block''' initializes the values for '''count''' and '''orgname''' as '''100 '''and '''IITM''' respectively. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| This '''static block''' initializes the values for '''count''' and '''orgname''' as '''100 '''and '''IITM''' respectively. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 239: | Line 192: | ||
click '''New-'''> '''Class''' and then type name as''' Demo'''. | click '''New-'''> '''Class''' and then type name as''' Demo'''. | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add one more class containing the '''main''' | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add one more '''class''' containing the '''main method'''. |
So right click on the''' default package,''' click '''New'''-> '''Class''' and then type the name as''' Demo.''' | So right click on the''' default package,''' click '''New'''-> '''Class''' and then type the name as''' Demo.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 252: | Line 202: | ||
− | Type '''main''' and then press''' | + | Type '''main''' and then press''' Ctrl+space''' to generate the''' main method.''' |
|- | |- | ||
Line 261: | Line 211: | ||
Type the following code to create an object''' s1.''' | Type the following code to create an object''' s1.''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 271: | Line 218: | ||
Type '''s1.showData() semicolon''' | Type '''s1.showData() semicolon''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''run''' button | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''run''' button | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us run the '''Demo''' program. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us run the '''Demo''' program. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 287: | Line 228: | ||
“constructor invoked” | “constructor invoked” | ||
+ | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| We can observe that '''static block''' is invoked before the '''constructor'''. | ||
+ | |- | ||
+ | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| | ||
Highlight '''100''' and '''IITM''' | Highlight '''100''' and '''IITM''' | ||
− | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"|The values of '''count''' and '''orgname''' are initialized as defined in the '''static block'''. | |
− | + | ||
− | + | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| | + | |
− | + | ||
− | + | ||
− | The values of '''count''' and '''orgname''' are initialized as defined in the '''static block'''. | + | |
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Go to '''StudentEnroll '''class. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Go to '''StudentEnroll '''class. | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us go back to the '''StudentEnroll ''' | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us go back to the '''StudentEnroll class'''. |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
Line 315: | Line 250: | ||
'''id equals IT01 semicolon''' | '''id equals IT01 semicolon''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 325: | Line 257: | ||
It indicates that an '''instance variable''' cannot be accessed inside a '''static block''' | It indicates that an '''instance variable''' cannot be accessed inside a '''static block''' | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| comment '''id=IT01;''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| comment '''id=IT01;''' | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us comment this line and proceed further. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us comment this line and proceed further. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch to slides | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch to slides | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us go back to the slides | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Let us go back to the slides | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 352: | Line 275: | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| A class can contain multiple '''static blocks.''' | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| A '''class''' can contain multiple '''static blocks.''' |
In such a case they are called only once in the order that they appear in the source code. | In such a case they are called only once in the order that they appear in the source code. | ||
− | |||
− | |||
− | |||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch to Eclipse | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch to Eclipse | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch back to Eclipse to verify it. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Switch back to Eclipse to verify it. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 383: | Line 300: | ||
Type the following code. | Type the following code. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 392: | Line 306: | ||
orgname="IITB"; | orgname="IITB"; | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| This '''static block''' initializes the values for '''count''' and '''orgname''' as 2'''00 '''and '''IITB '''respectively. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| This '''static block''' initializes the values for '''count''' and '''orgname''' as 2'''00 '''and '''IITB '''respectively. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 404: | Line 315: | ||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''run''' button | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Click on '''run''' button | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us run the '''Demo''' program again. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now let us run the '''Demo''' program again. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 422: | Line 328: | ||
− | The values of the '''static | + | The values of the '''static variables count '''and '''orgname''' are updated by the second '''static block'''. |
They are '''200 '''and '''IITB''' respectively. | They are '''200 '''and '''IITB''' respectively. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 447: | Line 350: | ||
* How to declare and define a '''static block''' and | * How to declare and define a '''static block''' and | ||
* How '''static blocks''' are invoked and executed''' ''' | * How '''static blocks''' are invoked and executed''' ''' | ||
− | |||
− | |||
|- | |- | ||
Line 456: | Line 357: | ||
− | * Design a class '''CarService''' to represent | + | * Design a class '''CarService''' to represent a Car Service Station |
− | + | * This class should contain variables to represent the following | |
− | + | ** Name of the Service Station | |
− | * This class should contain variables to | + | ** Car make, model, regno and status |
− | + | ** No of Cars in for Service | |
− | represent the following | + | ** No of Cars out after Service |
− | + | ||
− | * Name of the Service Station | + | |
− | * Car make, model, regno and status | + | |
− | * No of Cars in for Service | + | |
− | * No of | + | |
Line 488: | Line 384: | ||
* Define a '''constructor''' to initialize the values for '''instance variables ''' | * Define a '''constructor''' to initialize the values for '''instance variables ''' | ||
− | * Define a '''static block''' to initialize the values of | + | * Define a '''static block''' to initialize the values of '''static variables ''' |
Line 497: | Line 393: | ||
− | Define a '''static block''' to initialize the values of | + | Define a '''static block''' to initialize the values of '''static variables. ''' |
|- | |- | ||
Line 509: | Line 405: | ||
* Invoke the''' show( )''' method using all the objects and verify the results | * Invoke the''' show( )''' method using all the objects and verify the results | ||
− | |||
− | |||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Also create a''' Demo''' class containing the '''main''' method. | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Also create a''' Demo''' class containing the '''main''' method. | ||
Inside the main method, create a few objects of''' CarService '''and invoke the''' show( )''' method. | Inside the main method, create a few objects of''' CarService '''and invoke the''' show( )''' method. | ||
− | |||
− | |||
− | |||
|- | |- |
Latest revision as of 10:48, 31 December 2016
Title of script: Static Blocks
Author: Joms Antony
Keywords: Static blocks, Static variables, Instance variables, video tutorial
|
|
Slide 1 | Welcome to the Spoken Tutorial on Static blocks. |
Slide 2
Learning Objectives
|
In this tutorial we will learn:
|
Slide 3
Software Requirements
|
Here we are using
|
Slide 4
Prerequisites
|
To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.
|
Slide 5
Static Blocks
|
Let us learn about static blocks.
|
Slide 6
Static Blocks
|
If there are static blocks in a program, they are invoked before constructors.
|
In Eclipse IDE, create a project called StaticBlockDemo
|
Now, we will switch to Eclipse and create a new project called StaticBlockDemo.
|
Right click on src folder and click New-> Class
|
Right click on src folder and click New-> Class.
|
//copy the code below
private String id, name, branch; private static int count; private static String orgname; |
Now type the following code to represent the StudentEnroll class. |
Highlight count and orgname | Note that there are two static variables count and orgname. |
click on Source -> and select Generate Constructor using Fields | Now click on Source -> and select Generate Constructor using Fields |
Delete the super keyword | Delete the super keyword from the generated code. |
//Copy paste the following code
System.out.println("constructor invoked"); |
We want a message to be printed whenever the constructor is invoked.
|
//Copy paste the following code
public void showData() { System.out.println(id+" "+name+" "+branch+" "+count+ " "+ orgname); } |
Now we will add a method showData( ) to this class to print the values of the variables.
|
//Copy paste the code
static { count=100; orgname="IITM"; } |
Now we will add a static block to initialize the values of count and orgname.
|
Highlight orgname and count | The variables orgname and count are static variables.
|
Highlight count and orgname | This static block initializes the values for count and orgname as 100 and IITM respectively. |
//Copy paste the code inside the static method
System.out.println("static block-1 is invoked"); |
Inside this static block, type the following code to print “static block-1 is invoked”. |
Right click on the default package
click New-> Class and then type name as Demo. |
Now we will add one more class containing the main method.
|
Type main and then press ctrl+space | Inside this class we will have the main method.
|
//Copy the code
StudentEnroll s1=new StudentEnroll("IT101","ADIL","IT"); |
We will create an object of StudentEnroll class.
Type the following code to create an object s1. |
type s1.showData(); | Now let us invoke the showData method to print the values.
|
Click on run button | Now let us run the Demo program. |
Highlight “static block-1 is invoked”
“constructor invoked” |
We can observe that static block is invoked before the constructor. |
Highlight 100 and IITM |
The values of count and orgname are initialized as defined in the static block. |
Go to StudentEnroll class. | Now let us go back to the StudentEnroll class. |
Type id=IT01;
|
Let us see what happens if we initialise the value of id inside the static block.
id equals IT01 semicolon |
Show the error message | We can see that an error comes up.
|
comment id=IT01; | Now let us comment this line and proceed further. |
Switch to slides | Let us go back to the slides |
Slide 7
Static Blocks
|
A class can contain multiple static blocks.
|
Switch to Eclipse | Switch back to Eclipse to verify it. |
Go to the StudentEnroll class
//Copy paste the code static { count=200; orgname="IITB"; } |
Let us include one more static block after the existing one.
|
Highlight count=200;
orgname="IITB"; |
This static block initializes the values for count and orgname as 200 and IITB respectively. |
//copy paste this code
System.out.println("static block-2 is invoked"); |
Inside this static block type the following code. |
Click on run button | Now let us run the Demo program again. |
Highlight static block-1 is invoked
static block-2 is invoked
|
From the output, We can verify that the second static block is invoked after the first.
|
Slide 8
Summary
|
Let us summarize.
In this tutorial we have learnt
|
Slide 9A
Assignment
|
For the Assignment,
|
Slide 9B
|
Identify the instance variables and static variables.
|
Slide 9C
Inside the main method
|
Also create a Demo class containing the main method.
|
About Project
(retain the slide as in TEX file) |
The video at the following link summarizes the Spoken Tutorial Project.
|
About Workshops
(retain the slide as in TEX file) |
The Spoken Tutorial Project Team
• Conducts workshops using spoken tutorials and • Gives certificates on passing the online tests.
|
About NMEICT
(retain the slide as in TEX file) |
Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.
|
Contributor slide
(retain the slide as in TEX file) |
This script has been contributed by:
Dept. of Information Technology, Amal Jyothi College of Engineering.
|