Jobs: Using the class's static fields and constructors, we can track the number of objects created by a class. Write a class that can query it at any time "how many objects have you created?" ”。
Source:
1 ImportJava.util.Scanner;2 3 classsss123 {4 //The time of the variable that will be used to count times gives an initial value of 05 Public Static intTimes=0;6 sss123 () {7 //constructor, 1 more times per instance created8Times=times+1;9 }Ten } One A Public classStaticvalue { - Public Static voidMain (string[] args) { -sss123 S1 =Newsss123 (); thesss123 s2 =Newsss123 (); -sss123 s3 =Newsss123 (); - //you can see that currently we have created 3 objects manually, and then we call the static variable query of the Sss123 class to create how many objects -System.out.println ("The number of objects currently created is" +sss123.times); + } - } + A //Public class Staticvalue { at //Public static void Main (string[] args) { - //string Yorn=new string (); - //yorn= "Y"; - //int i=0; - //Scanner sc = new Scanner (system.in); - //While (Yorn.equals ("Y")) in // { - //System.out.println ("Creating an Object"); to // //I want to create an object once per loop, but I don't know how to express it . + //sss123 s = new sss123 (); - //System.out.println ("Create object Completion"); the //System.out.println ("The total number of objects currently created is" +sss123.times); * //System.out.println ("Do you want to continue creating objects?") Yes (Y)/No (N) "); $ //yorn=sc.nextline ();Panax Notoginseng // } - // } the //}
Classroom Examples:
1, the summary of Teststaticinitializeblock.java:
Source:
1 classRoot2 {3 Static{4SYSTEM.OUT.PRINTLN ("Static initialization block of root");5 }6 {7System.out.println ("Root's normal initialization block");8 }9 PublicRoot ()Ten { OneSYSTEM.OUT.PRINTLN ("Root parameter-free constructor"); A } - } - classMidextendsRoot the { - Static{ -SYSTEM.OUT.PRINTLN ("Static initialization block for mid"); - } + { -SYSTEM.OUT.PRINTLN ("Normal initialization block for mid"); + } A PublicMid () at { -SYSTEM.OUT.PRINTLN ("parameter-free constructor for mid"); - } - PublicMid (String msg) - { - //calling overloaded constructors in the same class through this in This(); -System.out.println ("Mid with parametric constructor, its parameter value:" +msg); to } + } - classLeafextendsMid the { * Static{ $SYSTEM.OUT.PRINTLN ("Static initialization block of the leaf");Panax Notoginseng } - { theSYSTEM.OUT.PRINTLN ("Plain initialization block of the leaf"); + } A PublicLeaf () the { + //A constructor that invokes a string argument in the parent class through Super - Super("Java Initialization sequence Demo"); $System.out.println ("The constructor that executes the leaf"); $ } - - } the - Public classTeststaticinitializeblockWuyi { the Public Static voidMain (string[] args) - { Wu NewLeaf (); - } About}
Operation Result:
Thus, static initialization blocks are executed in the following order: the static initialization blocks of the parent class are executed first, then the static initialization blocks of the subclasses are executed, then the normal initialization blocks and constructors of the parent class are executed, and then the normal initialization blocks and constructors of the subclasses are executed.
Java Field initialization law: Perform static initialization first, then perform normal initialization fast, execute the constructor, and then execute the initial value of the field.
The fault is that the constructor requires the passing of an shaping variable, but when used in the main function, it does not pass in the shaping variable, causing an error
Java classes and objects--summary of several classroom examples and assignments