Code blocks and code block static

Code blocks and code block static
syntax:
[Modifier] {class name of the class
{} // non-static block
static {// static block of code}
}

Code block is executed when?
(1) a static block, inClass initializationWhen executed, and onlyExecution time
(2)Non-static codeBlock, inExamples of initializationwhencarried out, Create an object once, and earlier than constructor
(member variables: We usually say that the property :)
(1) instance variables, most people say when a member variable, generally he was telling the instance variables
(2) class variables

public class TestBlock {
	public static void main(String[] args) {
		Init.test();//加载类Init到内存中初始化,需要执行一次静态代码块
		Init.test();//方法每调用一次执行一次内容
		Init.test();
	}
}
class Init{
	static {
		System.out.println("静态代码块");
	}
	public static void test(){
		System.out.println("静态方法");
	}
}

Results:
block static code
static methods
static methods
static method

public class TestBlock {
	public static void main(String[] args) {
		Init n1= new Init();//创建对象,需要将类加载到内存中,且优先于构造器,执行一次静态代码块初始化
		Init n2= new Init();
		Init n3 = new Init();
	}
}
class Init{
	public Init(){
		System.out.println("构造器");
	}
	static {
		System.out.println("静态代码块");
	}
	public static void test(){
		System.out.println("静态方法");
	}
}

Implementation of the results:
static block of code
constructor
constructor
constructor

public class TestBlock {
	public static void main(String[] args) {
		Init.test();
	}
}
class Init{
	public Init(){
		System.out.println("构造器");//只有创建对象才执行
	}
	static {//静态代码块
		System.out.println("静态代码块");
	}
	public static void test(){
		System.out.println("静态方法");
	}
	{//非静态代码块
		System.out.println("非静态代码块");//只有创建对象才执行
	}
}

Implementation of the results:
static code block
static method

public class TestBlock {
	public static void main(String[] args) {
		Init n1 = new Init();
		Init n2 = new Init();
		Init n3 = new Init();
	}
}
class Init{
	public Init(){
		System.out.println("构造器");//只有创建对象才执行
	}
	static {
		System.out.println("静态代码块");
	}
	public static void test(){
		System.out.println("静态方法");
	}
	{
		System.out.println("非静态代码块");//只有创建对象才执行
	}
}

Implementation of the results:
static code block
non-static block
constructor
non-static block
constructor
non-static block
constructor

public class Demo01 {
	public static void main(String[] args) {
		System.out.println(A1.max);
		
	}
}
class A1 extends A_Father{
	public static final int max = 300;
	//final修饰的常量不会发生类的初始化      
	//只有调用类的静态成员变量和静态方法 会发生类的初始化
	
	static {
		System.out.println("静态初始化A");
	}
	public A1(){
		System.out.println("创建A类的对象");
	}
	public static void test(){
		
	}
}
 

Implementation of the results:
300
+++++++++++++++++++++++++++++++++++++++++++++ +
class initialization of
each class, the compiler will automatically generate a < "clinit"> () method is called to initialize the class
class- "initalize, the following method is composed of two parts
displayed assignment (1) static variables
(2) static block statement in
two parts, who should perform the above
public class TestClinit {
public static void main (String [] args) {
a = a new new a (); // initialize class cause
}
}
class a {
static int NUM = getNum Private ();
static {
NUM ++;
System.out.println ( "static block of code: NUM =" + NUM);
}
public static int () {getNum
NUM ++;
System.out.println ( "getNum, = NUM "NUM +);
return 10;
}
}
// execution results:
getNum, NUM =. 1
Static block of code: num = 11
Here Insert Picture DescriptionHere Insert Picture Description
instance initialization

Published 10 original articles · won praise 0 · Views 98

Guess you like

Origin blog.csdn.net/xiaobao1352/article/details/104208282