You have created a number of objects?

       Static fields and constructor of the class, a class can track the number of objects created. Please write a class at any time to query it. "You have created a number of objects?

Package ppt_test; 
public class Test4 { 
	int value; 
	static int A = 0; 
	Test4 () { 
		A ++; 
	} 
	public static void main (String args []) 
	{ 
			Test4 A = new new Test4 (); 
			Test4 B = new new Test4 (); 
			System.out.println ( "you have created the" + a.a + "objects"); 
	} 
}

 

        Need only implement this is very simple to achieve accumulation of the variables in the constructor in a static variable defined in the class, then the output can in the main function.

 

Guess you like

Origin www.cnblogs.com/xp-thebest/p/11704175.html