We know that a class can have only declarative statements but if we want to put some other statements then that can be done by using BLOCKS .
There are two types of blocks :
(i) named blocks or methods
(ii) anonymous blocks
We are already familiar with the first one i.e. by using methods .Ex:
class sample{
static void display()
{
System.out.println("Hello !!");
}
public static void main(String args[ ])
{
display();
}
}
Output : Hello !!
Now if we need to put the print statement inside the class and without using methods then we use anonymous blocks.
Ex: class sample{
{
System.out.println("Anonymous Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s= new sample();
}
}
Output : Main Method !!
Anonymous Block !!
Note : Whenever objects are created both constructor and anonymous blocks are called automatically. But the anonymous block will always run before the constructor.
-----------------------------------------------------------------------------------------------------------------------------------
Ex: class sample{
sample()
{
System.out.println("Default Constructor !!");
}
{
System.out.println("Anonymous Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s= new sample();
}
}
Output : Main Method !!
Anonymous Block !!
Default Constructor !!
Anonymous block is also called an initializer block .
Note : If we have a large number of constructors and if they share a common set of lines then we can use an anonymous block to write those common lines and they will be called for each and every object that is constructed.
-----------------------------------------------------------------------------------------------------------------------------------
If we need to initialize a static data member inside a constructor then we have two drawbacks -
(I) Static data member becomes somewhat object independent.
(II) The data member is again and again initialized whenever a object is created.
Solution : We use static initializer blocks.
Ex: class sample{
sample()
{
System.out.println("Default Constructor !!");
}
{
System.out.println("Anonymous Block !!");
}
static
{
System.out.println("Static Initializer Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s1= new sample();
sample s2=new sample();
}
}
Output : Static Initializer Block !!
Main Method !!
Anonymous Block !!
Default Constructor !!
Anonymous Block !!
Default Constructor !!
Note: (I) Static block always runs first irrespective of the creation of objects.
(II) Static block runs only once but non-static runs every time an object is created.
-----------------------------------------------------------------------------------------------------------------------------------
There are two types of blocks :
(i) named blocks or methods
(ii) anonymous blocks
We are already familiar with the first one i.e. by using methods .Ex:
class sample{
static void display()
{
System.out.println("Hello !!");
}
public static void main(String args[ ])
{
display();
}
}
Output : Hello !!
Now if we need to put the print statement inside the class and without using methods then we use anonymous blocks.
Ex: class sample{
{
System.out.println("Anonymous Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s= new sample();
}
}
Output : Main Method !!
Anonymous Block !!
Note : Whenever objects are created both constructor and anonymous blocks are called automatically. But the anonymous block will always run before the constructor.
-----------------------------------------------------------------------------------------------------------------------------------
Ex: class sample{
sample()
{
System.out.println("Default Constructor !!");
}
{
System.out.println("Anonymous Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s= new sample();
}
}
Output : Main Method !!
Anonymous Block !!
Default Constructor !!
Anonymous block is also called an initializer block .
Note : If we have a large number of constructors and if they share a common set of lines then we can use an anonymous block to write those common lines and they will be called for each and every object that is constructed.
-----------------------------------------------------------------------------------------------------------------------------------
If we need to initialize a static data member inside a constructor then we have two drawbacks -
(I) Static data member becomes somewhat object independent.
(II) The data member is again and again initialized whenever a object is created.
Solution : We use static initializer blocks.
Ex: class sample{
sample()
{
System.out.println("Default Constructor !!");
}
{
System.out.println("Anonymous Block !!");
}
static
{
System.out.println("Static Initializer Block !!");
}
public static void main(String args[ ])
{
System.out.println("Main Method !!");
sample s1= new sample();
sample s2=new sample();
}
}
Output : Static Initializer Block !!
Main Method !!
Anonymous Block !!
Default Constructor !!
Anonymous Block !!
Default Constructor !!
Note: (I) Static block always runs first irrespective of the creation of objects.
(II) Static block runs only once but non-static runs every time an object is created.
-----------------------------------------------------------------------------------------------------------------------------------
No comments:
Post a Comment