Instance Static Block In Java
The Purpose of instance Static block is to initialize the instance "Data Members" of a class .In other words The Functionality of instance block is similar to the functionality of default constructor of a class .
The execution behavior of instance block is that "instance block called will be called Java virtual mechanic (JVM) each every time whenever object is created and it is executed by Jvm before executing constructor and other executing static block ".
{
Block of statement (s) initialization
}
Instance block will be written always within the Class definition .
Q. Write a Java Program which illustrate initializing the data instance data member by instance block .
Program:-
Class Student
{
int stno;
String name;
static
{
System.out.println("static block called");
}
{
System.out.println("Instance block called ");
stno=10;
name="SATYA";
System.out.println("val of stno="+ stno) ;
System.out.println("val of stname="+ name);
}
Student()
{
System.out.println("constructor called");
}
}
Class StudentInfo
{
public static void main(String args[])
{
Student s1 = new Student();
Student s2 = new Student();
}
}
No comments:
Post a Comment
Thanks for visiting my site..