Difference between revisions of "Java/C3/Static-Blocks/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
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'''
  
initializing values of '''static variables'''  
+
* A '''static block''' is declared using the '''static''' keyword
  
* A '''static block''' is declared using the  
+
* '''Static blocks''' are executed when the '''class''' is loaded in memory  
 
+
'''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
+
 
+
  
  
 +
So type the following code.
  
 
|-
 
|-
Line 214: Line 171:
  
  
The variables '''orgname''' and '''count''' shown in this example are '''static''' '''variables'''.
+
|-
 +
| 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''' method
+
| 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''' ctrl+space''' to generate the''' main method.'''
+
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;"| We can observe that '''static block''' is invoked before the '''constructor'''.
+
 
+
 
+
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 '''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 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''' '''variables''' '''count '''and '''orgname''' are updated by the second '''static block'''.
+
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
  
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 cars out after Service''' '''
+
  
  
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 s'''tatic variables '''
+
* Define a '''static block''' to initialize the values of '''static variables '''
  
  
Line 497: Line 393:
  
  
Define a '''static block''' to initialize the values of s'''tatic variables. '''
+
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


Visual Cue
Narration
Slide 1 Welcome to the Spoken Tutorial on Static blocks.
Slide 2

Learning Objectives

  • What are static blocks
  • How to declare static blocks
  • How to use static blocks
In this tutorial we will learn:
  • What are static blocks
  • How to declare static blocks and
  • How to use static blocks
Slide 3

Software Requirements

  • Ubuntu 14.04
  • JDK 1 .7
  • Eclipse 4.3.1
Here we are using
  • Ubuntu 14.04
  • JDK 1 .7 and
  • Eclipse 4.3.1
Slide 4

Prerequisites

  • Basic knowledge of Java and Eclipse IDE
  • Knowledge of instance variables, static variables and static methods in Java
  • For relevant tutorials, please visit http://www.spoken-tutorial.org
To follow this tutorial, you must have knowledge of basics of Java and Eclipse IDE.


You should also have the knowledge of instance variables, static variables and static methods in Java.


If not, for relevant Java tutorials, please visit the link shown.

Slide 5

Static Blocks

  • 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
Let us learn about static blocks.
  • 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.
Slide 6

Static Blocks

  • Static blocks are invoked before constructors
  • Instance variables cannot be accessed inside static blocks
If there are static blocks in a program, they are invoked before constructors.


We cannot access instance variables inside a static block.

In Eclipse IDE, create a project called StaticBlockDemo


Now, we will switch to Eclipse and create a new project called StaticBlockDemo.


Inside this project, we will create the necessary classes to demonstrate the usage of Static Blocks.

Right click on src folder and click New-> Class


Type the name of the class as StudentEnroll.

Right click on src folder and click New-> Class.


Type the name of the class as StudentEnroll and press Enter.

//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.


So inside this constructor, type the following code to print “Constructor 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.


So type the following code.

//Copy paste the code

static {

count=100;

orgname="IITM";

}

Now we will add a static block to initialize the values of count and orgname.


Type the following code.


Highlight orgname and count 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.

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.


So right click on the default package, click New-> Class and then type the name as Demo.

Type main and then press ctrl+space Inside this class we will have the main method.


Type main and then press Ctrl+space to generate 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.


Type s1.showData() semicolon

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.


Inside the static block type,

id equals IT01 semicolon

Show the error message We can see that an error comes up.


It indicates that an instance variable cannot be accessed inside a static block

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

  • There can be multiple static blocks inside a class
  • They are called only once in the order that they appear in the source code


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.

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.


Type the following code.

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


Highlight 200 IITB

From the output, We can verify that the second static block is invoked after the first.


The values of the static variables count and orgname are updated by the second static block.


They are 200 and IITB respectively.

Slide 8

Summary

  • What is a static block
  • How to declare and define a static block
  • How static blocks are invoked and executed


Let us summarize.

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
Slide 9A

Assignment


  • Design a class CarService to represent a Car Service Station
  • This class should contain variables to represent the following
    • Name of the Service Station
    • Car make, model, regno and status
    • No of Cars in for Service
    • No of Cars out after Service


For the Assignment,


This assignment is a continuation of the Static Methods assignment.


Make sure that you have completed the Static Methods assignment.


As before design the class CarService as specified in this slide.

Slide 9B
  • Identify the instance variables and static variables
  • Define a constructor to initialize the values for instance variables
  • Define a static block to initialize the values of static variables


Identify the instance variables and static variables.


Define a constructor to initialize the values for instance variables.


Define a static block to initialize the values of static variables.

Slide 9C
  • Also create a Demo class containing the main method

Inside the main method

  • Create a few objects of CarService
  • Invoke the show( ) method using all the objects and verify the results
Also create a Demo class containing the main method.


Inside the main method, create a few objects of CarService and invoke the show( ) method.

About Project

(retain the slide as in TEX file)

The video at the following link summarizes the Spoken Tutorial Project.


Please download and watch it.

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.


For more details, please write to us.

About NMEICT

(retain the slide as in TEX file)

Spoken Tutorial Project is funded by the NMEICT, MHRD, Government of India.


More information on this Mission is available at the link shown.

Contributor slide

(retain the slide as in TEX file)

This script has been contributed by:

Dept. of Information

Technology, Amal Jyothi College of Engineering.


This is Priya from IIT Bombay. Thanks for joining.

Contributors and Content Editors

Nancyvarkey, Priyacst