0% found this document useful (0 votes)
3 views2 pages

Java Instance Initializer Block Explained

Instance block

Uploaded by

Yash Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

Java Instance Initializer Block Explained

Instance block

Uploaded by

Yash Pandey
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Instance Initializer Block in Java

In Java, Instance Initializer block is used to initialize the instance data member. It run each time when object of the class is
created.
class Test class Test
{ {
int hat; Test()
{
{ [Link]("Constructor");
hat = 121; }
}
{
Test() [Link]("Instance Initializer");
{ }
[Link](hat);
} public static void main(String... start)
{
public static void main(String... start) Test t1 = new Test();
{ [Link]("Main");
Test t1 = new Test(); }
Test t2 = new Test(); }
Test t3 = new Test();
}
}

class Exam class Exam


{ {
Exam() Exam()
{ {
[Link]("Super Constructor"); [Link]("Super Constructor");
} }
} {
class Test extends Exam [Link]("Super Initializer");
{ }
Test() }
{ class Test extends Exam
[Link]("Constructor"); {
} Test()
{
{ [Link]("Constructor");
[Link]("Instance Initializer"); }
}
{
public static void main(String... start) [Link]("Instance Initializer");
{ }
Test t1 = new Test();
[Link]("Main"); public static void main(String... start)
} {
} Test t1 = new Test();
[Link]("Main");
}
}
abstract class abc
{
{
[Link]("Abstract Class Instance Block");
}

abc()
{
[Link]("Abstract Class Constructor");
}

public static void main(String arg[])


{
abc a1=new abc(){
{
[Link]("Anonymous Class Instance Block");
}
};
[Link]("Main method");
}
}

You might also like