Java Fifth week hands-on brain

Source: Internet
Author: User

Why does the following code fail to compile? Where did it go wrong?

Cause Analysis: This is a common construction error, the wrong to build a construction method, resulting in the system can not be properly constructed variable, the system default construction method does not work

Two

Package demo;  Public class Test {    publicstaticvoid  main (string[] args) {        // TODO Auto-generated method stubs        Foo obj1=new  foo ();        Foo obj2=new  foo ();        System. out. println (obj1==obj2);             }
Package demo; Public classChushihua { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsInitializeblockclass obj=NewInitializeblockclass (); System. out. println (Obj.field); Obj=NewInitializeblockclass ( -); System. out. println (Obj.field); }    }
Package demo; class Foo {    int  value;}

Cause: Object variables cannot be judged equal to each other

Third, run the Teststaticinitializeblock.java example, observe the output, and summarize the "Order of execution of static initialization blocks".

Source:

Package demo;classRoot {Static{System. out. println ("static initialization block of root"); } {System. out. println ("common initialization block for root"); }     PublicRoot () {System. out. println ("The parameterless constructor of root"); }}
Package demo;classMid extends root{Static{System. out. println ("static initialization block for mid"); } {System. out. println ("normal initialization block for mid"); }     PublicMid () {System. out. println ("parameter-free constructors for mid"); }     PublicMid (String msg) {//calling overloaded constructors in the same class through this         This(); System. out. println ("with parameter constructors for mid, the parameter values are:"+msg); }}
Package demo;classLeaf extends mid{Static{System. out. println ("static initialization block of the leaf"); } {System. out. println ("ordinary initialization blocks of the leaf"); }         PublicLeaf () {//A constructor that invokes a string argument in the parent class through SuperSuper"Java Initialization Sequence demo"); System. out. println ("the constructor that executes the leaf"); }}
Package demo;  Public class Teststaticinitializeblock {    publicstaticvoid  main (string[] args) {         New Leaf ();    }}

Reason:

Static blocks run when the first object is created, the initialization block runs at each object creation time, the difference is that the static block executes only once, the memory in the static area, the initialization block is executed once per object construction, the operation of the NEI in the user area

Four

Source:

Package demo; class Initializeblockclass {        field= $;    }      Public int field=;      Public Initializeblockclass (int  value)    {        this. field=value;    }       Public Initializeblockclass ()    {}}
Package demo; Public classChushihua { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsInitializeblockclass obj=NewInitializeblockclass (); System. out. println (Obj.field); Obj=NewInitializeblockclass ( -); System. out. println (Obj.field); }}

Program:

Cause Analysis:

Initialization rules for class fields: 1. Execute the default value or initialization block specified when the class member is defined, and which one you want to see is in front of the row. 2: The constructor of the execution class;

Initialization of the first time is the initialization block of the class, the second time with the public int initialization, so the second time overwrite the first, output 100, the main function initializeblockclass the initial value 300, so cover 100, the output is 300

Five

Source:

Package demo;//import Jxlpacakge.mypackageclass; Public classTTT {protectedString Alpha;  Public voidDT () {System. out. println ("ABCD"); }         Public Static voidMain (string[] args) {TTT T=NewTTT ();            T.dt (); }}

Program:

Cause Analysis:

The program will eventually be executed in memory, and the variable can be accessed only if it occupies a place in memory.

Static members of a class (variables and methods) belong to the class itself, which allocates memory when the class is loaded, can be accessed directly through the class name, and non-static members (variables and methods) are objects of the class, so memory is allocated only when the class's object is generated (an instance of the class is created) and then accessed through the object (instance)

Accessing its non-static members in a static member of a class is an error because a static member of the class already exists when the non-static member of the class does not exist, and accessing something that does not exist in memory is of course an error.

Java Fifth week hands-on brain

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.