Difference between revisions of "Java/C3/Static-Blocks/English"
(Created page with "'''Title of script:''' Static Blocks '''Author:''' Joms Antony '''Keywords:''' Static blocks, Static variables, Instance variables, video tutorial {| style="border-spacin...") |
|||
Line 96: | Line 96: | ||
|- | |- | ||
| style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 6''' | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 6''' | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
'''Static Blocks''' | '''Static Blocks''' | ||
Line 236: | Line 208: | ||
} | } | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add a static block to initialize the values of '''count''' and '''orgname.''' | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| Now we will add a '''static block''' to initialize the values of '''count''' and '''orgname.''' |
Line 242: | Line 214: | ||
− | + | The variables '''orgname''' and '''count''' shown in this example are '''static''' '''variables'''. | |
+ | |||
+ | |||
+ | 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 334: | Line 312: | ||
− | Inside the static block type, | + | Inside the '''static block''' type, |
'''id equals IT01 semicolon''' | '''id equals IT01 semicolon''' | ||
Line 366: | Line 344: | ||
|- | |- | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 7''' |
'''Static Blocks''' | '''Static Blocks''' | ||
Line 413: | Line 391: | ||
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 444: | Line 422: | ||
− | 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'''. |
Line 453: | Line 431: | ||
|- | |- | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 8''' |
'''Summary''' | '''Summary''' | ||
Line 473: | Line 451: | ||
|- | |- | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 9A''' |
'''Assignment''' | '''Assignment''' | ||
Line 480: | Line 458: | ||
* 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 | * This class should contain variables to | ||
Line 504: | Line 482: | ||
|- | |- | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 9B''' |
* Identify the '''instance variables''' and '''static variables''' | * Identify the '''instance variables''' and '''static variables''' | ||
Line 522: | Line 500: | ||
|- | |- | ||
− | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide | + | | style="border:1pt solid #000001;padding-top:0cm;padding-bottom:0cm;padding-left:0.173cm;padding-right:0.191cm;"| '''Slide 9C''' |
* Also create a''' Demo''' class containing the main method | * Also create a''' Demo''' class containing the main method |
Revision as of 17:29, 26 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
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.
|