What is
This Block or Instance Initializer Or IIB
?
This is an anonymous block, without any prefix
or modifier, present inside class. It is used to initialize instance data members
(instance variables).
2.
When IIB is invoked?
IIB will be invoked for each object creation.
i.e for each time when the instance of the class is created.
3.
Which is invoked first IIB or Constructor?
IIB is called first. IIB is invoked before the
constructor of the class are invoked.
4.
Order of execution of IIB from subclasses?
When the object of the subclasses are created,
super class IIB will invoked first. Subclass Constructor will call super()
class constructor first. Like the same way Super class IIB will execute before
super class constructor.
Order of execution will be like below during
subclass object creation :
Super class IIB à super class
constructor --> subclass IIB --> subclass constructor
5.
How compiler will see the code ?
Consider we have below code :
public class ThisIntializer extends ThisSuper
{
{
System.out.println("ThisIntializer :
Subclass this block 1........");
}
ThisIntializer() {
System.out.println("ThisIntializer :
Subclass Constructor .......");
}
During the compilation compiler will see the
code as below:
public class ThisIntializer extends ThisSuper
{
ThisIntializer() {
Super()
{
System.out.println("ThisIntializer :
Subclass this block 1........");
}
System.out.println("ThisIntializer
: Subclass Constructor .......");
}
6.
What is the use of IIB? And where it is used?
IIB is using mainly for initializing instance
data member (instance variable – non static variables). IIB is doing the same
as what constructor is doing.
But we used the same in some places where we
want to do below activities:
·
If you want to do some operation before
assigning values to instance data members (instance variables). Like array,
List initialization or for doing complex calculation.
·
For doing error handling by catching
exceptions and using finally.
·
As a initializer for anonymous inner
classes.(we can’t declare any constructor inside the anonymous inner classes).
7.
If Constructor is able to do the
initialization of instance variable, why we need IIB ?
The assigning and error handling that is
mentioned in above (6th question) will be able to achieve with constructor also, but if the class is having more than one
constructor, we would have to repeat this code in each constructor. At this
point of time IIB will be good to use.
It is mainly useful in the case of
anonymous inner classes, where the is no constructor available for
initialization of variables.
Example for anonymous inner classes using
he IIB ?
……………………………..
new Thread(new Runnable(){
Path
file = Paths.get("./test.txt");
{
try{
InputStream
in = Files.newInputStream(file);
BufferedReader reader =
new BufferedReader(new InputStreamReader(in));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
}
catch (IOException x) {
System.err.println(x);
}
}
@Override
public void run() {
System.out.println("run ");
}
}).start();
………………………….
We can handle exception from IIB. But only
thing is that if we are throwing any checked exception from IIB , then that
should be declared by the constructors.
E.g
public class innerClass {
StringReader reader = null;
{
reader = new StringReader(new String("test"));
try {
reader.read();
} catch (IOException e) {
throw e;
}
}
innerClass() throws IOException {
}
In the above code if we don’t throw an
Exception from constructor then the code will not compile.
Example for Exception handling from IIB , for
anonymous inner classes ?
Instance initializers in anonymous inner
classes can throw any exception. See the question number 8.
No comments:
Post a Comment