Java classes and objects post-lesson jobs

Source: Internet
Author: User
Tags stub

1.

 Public classTest {/*** @paramargs*/   Public Static voidMain (string[] args) {//TODO auto-generated Method StubFoo obj1=NewFoo (); Foo onj2=NewFoo (); System.out.println (Obj1==obj2); }}classfoo{intvalue=100;}

Summarize:

when "= =" is applied to the original data type variable, is the data saved by the comparison variable equal

when "= =" is applied to a reference type variable, it is compared whether the two variables refer to the same object.

The reference represents the address, so "= =" is actually equivalent to comparing the address of an object saved in two reference type variables.

2. Why is the following code not compiled? Where did it go wrong?

Conclusion: If a class provides a custom construction method, it will cause the system to no longer provide a default construction method.

3.

classinitializeblockclass{{field=200;}  Public intfield=100;  PublicInitializeblockclass (intValue) { This. field=value;}  PublicInitializeblockclass () {}} Public classTest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubInitializeblockclass obj=NewInitializeblockclass ();                System.out.println (Obj.field); Obj=NewInitializeblockclass (300);           System.out.println (Obj.field); }  }

100

300

Summary rule:

(1) The default value specified when executing the class member definition or the initialization block of the class, exactly which one you want to see is "in front".

(2) Executes the constructor of the class .

(3) The initialization blocks of the class do not receive any arguments, and they are executed whenever an object of the class is created. Therefore, it is appropriate to encapsulate those "code that must be executed when the object is created."

4.

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 (); }}

Results:

Summary: execution order of static initialization blocks

Static initialization blocks are executed only once.

When you create an object of a subtype, it also causes the execution of the static initialization block of the parent type.

5. Static methods only allow access to static data, so how do you access the instance members of the class in a static method (that is , a field or methodthat does not have the static keyword attached)?

 Public class buttonfactory {privatestaticnull;  Public Static   buttonfactory getinstance () {    if(null = = _instance    ) New buttonfactory ();     return _instance;}}

6 using static fields and constructors for classes, 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?" "

 Public classObjectnumber { Public Static voidMain (string[] args) {@SuppressWarnings ("Unused") Number N1=NewNumber (); @SuppressWarnings ("Unused") Number N2=NewNumber (); @SuppressWarnings ("Unused") Number N3=NewNumber (); System.out.println ("You have now created" +number.count+ "objects"); }}classnumber{Static intcount;  PublicNumber () {count++;} intGetCount () {returncount;}}

Java classes and objects post-lesson jobs

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.