Java/C3/Static-Blocks/English

From Script | Spoken-Tutorial
Revision as of 10:48, 31 December 2016 by Nancyvarkey (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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