1. Why is the following code not compiled? Where did it go wrong?
Cause: Object variables cannot be equal
2. When there is an inheritance relationship between multiple classes, creating a subclass object causes the parent class to initialize the execution of the block.
3. Static initialization of the execution order of the blocks.
If the static modifier is used when defining the initialization block, it becomes a static initialization block. When static initialization blocks are class-dependent, the system executes static initialization blocks during the class initialization phase, rather than when the object is created.
Static initialization blocks are always executed first than normal initialization blocks. Similar to ordinary initialization blocks, when the system executes static initialization blocks during the class initialization phase, it not only executes static initialization blocks of this class, but also traces back to the Java.lang.Object class. Executes the static initialization block in the Java.lang.Object class first, then executes the static initialization block of its parent class, and finally executes the static initialization block of the class. After this process, the initialization of the class is completed.
4. Please write a class, and at any time you can query it "how many objects have you created?"
Source:
Package Boke;
public class student{
public static void Main (string[] args) {
Information A = new information ();
A.outputinformation ();
Information B = new information ();
B.outputinformation ();
} }
Class information{
static int num=0;
Information ()
{num++;
}
public void Outputinformation ()
{
System.out.println ("You've created a +num+" Object!) ");
}
}
Hands-on brain and homework after class