(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Title of script: Non-static block
Author: Arya Ratish
Keywords: video tutorial, non-static block, instance
Visual Cue
|
Narration
|
Slide 1
Opening slide
|
Welcome to the Spoken Tutorial on Non-static block in java.
|
Slide 2
Learning Objectives
|
In this tutorial we will learn
- About non-static block
- When is a non-static block executed?
- Simple example of non-static block
- Why we need constructors?
|
Slide 3
System Requirements
|
Here we are using
- Ubuntu version 11.10
- Java Development Environment jdk 1.6
- Eclipse 3.7
|
Slide 4
Prerequisites
|
To follow this tutorial you must know
- To create a constructor in Java using Eclipse
If not, for relevant tutorials please visit our website which is as shown,
(http://www.spoken-tutorial.org)
|
Slide 5
Non-static block
|
Now we will see what a non-static block is.
Any code written between two curly brackets is a non-static block.
We can see the syntax here.
|
Slide 6
When is a non-static block executed?
|
A non-static block is executed for each object that is created.
It executes before the constructor's execution.
It can initialize instance member variables of the class.
Any other execution like calculation could also be given in the block.
Now, let us switch to Eclipse and try to use a non-static block.
|
Switch to Eclipse
|
I have already opened a class named NonStaticTest in Eclipse.
I also have a created a class named A .
Inside class A , I will first create a variable of type int.
So type int a semicolon
Within curly brackets type System dot out dot println within brackets and double quotes Non static block of an instance of Class A semicolon.
Then type System dot out dot println within brackets and double quotes The value of a is plus a semicolon.
Now we will declare a constructor.
So type public A opening and closing brackets, then curly brackets open and press Enter.
Then type within brackets System dot out dot println within brackets and double quotes Constructing object of type A.
Then type System dot out dot println within brackets and double quotes The value of a is plus a semicolon.
Now, inside the class NonStaticTest in Eclipse let us create an object of the class A.
So type A space a1 equal to new space A opening and closing brackets.
Next line, type A space a2 equal to new space A opening and closing brackets.
Save and run the file.
|
Highlight the output.
|
Now, let us see the output.
We get the output as follows:
|
Open class A.
Highlight as you explain.
|
As we can see when the first object is created, the non-static block is executed.
The instance variable is initialized to 0.
Only after that the constructor is executed.
The instance variable is again initialized to 0.
Then again when the second object is created, the non-static block is executed.
And the process is repeated.
|
Open class A .
Type as you narrate.
|
We can have multiple non-static blocks in a class.
In this case they execute in the sequence in which they appear in the class.
We can try that now.
Include one more block after the first one.
So again type inside curly brackets.
Within curly brackets type System dot out dot println within brackets and double quotes Second Non static block of an instance of Class A semicolon.
Then type System dot out dot println within brackets and double quotes The value of a is plus a semicolon.
Now save and run the file.
|
Highlight the output.
|
We get the output as follows.
As we can see after the first block is executed, the second is executed.
Only then the constructor is executed.
|
Slide 7
Why do we need constructors?
|
You might feel why do we need constructors.
The answer is we don't need the default constructor.
But the non-static block cannot be parameterized.
You cannot have objects taking values from out side.
So non-static block is not a substitute for constructor.
|
Slide 8
Summary
|
In this tutorial we learnt :
|
Slide 9
Assignment
|
For self assessment,
- create a class named B.
- Create a non-static block and a constructor as shown in the tutorial.
- Create an object of class B in the class NonStaticTest already created.
- Check the output.
|
Slide 10
About Slide
|
To know more about the Spoken Tutorial Project
- If you do not have good bandwidth, you can download and watch it
|
Slide 11
About Slide
|
The Spoken Tutorial Project Team
- Conducts workshops using spoken tutorials
- Gives certificates for those who pass an online test
- For more details, please write to contact@spoken-tutorial.org
|
Slide 12
Acknowledgment
|
Spoken Tutorial Project is a part of the Talk to a Teacher project
- It is supported by the National Mission on Education through ICT, MHRD, Government of India
- More information on this Mission is available at
|
|
We have come to the end of this tutorial.
Thanks for joining.
This is Arya Ratish signing off.
Jai Hind.
|