Non Static Block

Non Static Block

  • Used to initialize non static data member.
  • Non static block execute each time when object is created.

What is the Use of Non Static block ?

  • To initialize non static data member.
Non Static block Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package block;
import thread.mainclass;
public class Nonstaticblock 
{
	//defining non static block
	{
	  System.out.println("running non static block");
	}
 
	public static void main(String[] args) 
	{
		System.out.println("main method executed");
		//craeting instance
		Nonstaticblock b1=new Nonstaticblock();
 
		//creating second instance
		Nonstaticblock b2=new Nonstaticblock();
	}
}
output:
 
main method executed
running non static block
running non static block

One Reply to “Non Static Block”

  1. BestChauncey

    I see you don’t monetize your website, don’t waste your traffic, you can earn extra cash
    every month because you’ve got hi quality content.
    If you want to know how to make extra money, search for:
    Ercannou’s essential adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *