Java/C3/Static-Blocks/English
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
initializing values of static variables
static keyword
class is loaded in memory |
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 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.
|
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
a Car Service Station
represent the following
|
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.
|