1. Please run and enter the following code to get what results
public class Test { publicstaticvoid main (string[] Args) { Foo obj1=New Foo (); Foo obj2=new foo (); System.out.println (obj1= =obj2);} } class foo{ int value=100;}
Results:
Analysis: two not part of the same object
2. Why is the following code not compiled? Where did it go wrong?
public class Test { publicstaticvoid main (string[] Args) { Foo obj1=New Foo (); }} class foo{ int value; public Foo (int Initvalue) { value=initvalue; }}
Cause: the constructor foo was not Created. If a class provides a custom construction method, the default construction method is no longer provided by the System.
3.java Field Initialization rules
It says to look at the assignment of the first defined and its constructors.
4. Execution order of static initialization blocks
classroot{Static{System.out.println ("static initialization block of root"); } {System.out.println ("root's Normal Initialization block"); } publicRoot () {System.out.println ("root parameterless constructor"); }}classMidextendsroot{Static{System.out.println ("static initialization block for mid"); } {System.out.println ("normal initialization block for mid"); } publicMid () {System.out.println ("no Parametric constructor for mid"); } publicMid (String Msg) {//calling overloaded constructors in the same class through this this(); System.out.println ("mid with parametric constructor, its parameter value:" +msg); }}classLeafextendsmid{Static{System.out.println ("static initialization block of the leaf"); } {System.out.println ("plain initialization block of the leaf"); } publicLeaf () {//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"); }} public classteststaticinitializeblock{ public Static voidmain (string[] Args) {NewLeaf (); }}
Executes only once, but when the child class creates the object, the parent class static initialization block also executes
The specific execution order Is: initialize the static initialization block of the parent class (the highest Level) first, The parameterless constructor of the parent class, the static initialization block of the subclass, the parameterless constructor of the subclass;
Then is the normal initialization block of the parent class, the normal initialization block of the subclass, the parameter constructor of the parent class, and the parameter constructor of the Subclass.
5. How to access instance members of a class in a static method
Refer to the code given by Baidu
public classTry {Private StaticTry _instance =NULL; public StaticTry getinstance () {if(NULL==_instance) _instance=NewTry (); return_instance; } public Static voidmain (string[] Args) {System.out.println (_instance); }}
Accessing an instance variable of a class in a static method requires the instantiation of the class first, in addition to the static modification, you can first create an object, call the member method and the variable through the object
How is the boxed and unboxing of the 6.Integer class implemented?
7. How many objects have you created?
public classObjectnumber { public Static voidmain (string[] Args) {Num obj1,obj2,obj3; Obj1=NewNum (); Obj2=NewNum (); Obj3=NewNum (); Obj1.getnum (); } }classnum{ public Static intA=0; {a++; } public voidgetnum () {System.out.println ("+a+" Object "created"); } }
JAVA-04 classes and Objects class exercises