JAVA-04 classes and Objects class exercises

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.