Think in Java (ii): initialization and cleanup

Source: Internet
Author: User

1. Differentiate Overloaded methods:

The difference in the number of parameters can be distinguished by two methods, except that in normal circumstances, do not do so. Because this makes the code difficult to maintain, the overloaded method cannot be distinguished by a return value type: Because I suppose I call F () directly, Java does not know that it should call that

public void F () {}public int f () {return 1;}

2. It is only necessary to use Thiskeyword when you need to understand the reference to the current object. For example, when a reference to the current object needs to be returned, it is often written in the return statement:

//Because increment () returns a reference to the current object through Thiskeyword. So very easy to run multiple operations on the same object in a single statement


3. Why is the Finalize () method required?
It is not always safe to discard an object after it has been exhausted, of course. Java has a garbage collector responsible for reclaiming memory resources occupied by useless objects, but there are special cases: suppose your object (not using new) obtains a "special" area of memory because the garbage collector only knows to release the memory allocated through new. So it doesn't know how to release this "special" memory for that object.

To cope with this situation, Java agrees to define a method named Finalize () in the class .

Finalize () should not be used as a common cleanup method .



Finalize () is required because it is possible to allocate memory in a way similar to the C language, rather than the usual practice in Java. This kind of oh concept situation mainly occurs when using the "local method". A local method is a way to invoke non-Java code in Java.

Neither "garbage collection" nor "finalization method" is guaranteed to occur, assuming the Java virtual machine is not running out of memory, it is not a waste of time
Go to run garbage collection to recover the memory.

4. Sequence of initialization:
Within the class, the order in which the variables are defined determines the order in which they are initialized, even if the variable definitions are scattered between the method definitions, they will still be initialized before whatever method (including constructor) is called.

Class Window {window (int marker) {print ("window (" + marker + ")");}} Class House {window w1 = new Window (1),//Before Constructorhouse () {//Show that we ' re in the Constructor:print ("house") "); W3 = new Window (33); Reinitialize W3}window w2 = new Window (2); After Constructorvoid f () {print ("F ()");} Window W3 = new window (3); At End}public class Orderofinitialization {public static void main (string[] args) {House H = new House (); H.f ();//Show s that construction was done}}//Output:window (1) window (2) window (3) House () window () f () *///:~

5. Initialization of static data:
Class Bowl {Bowl (int marker) {print ("Bowl (" + marker + ")");} void F1 (int marker) {print ("F1 (" + marker + ")");}} Class Table {static Bowl bowl1 = new Bowl (1); Table () {Print ("table ()"); Bowl2.f1 (1);} void F2 (int marker) {print ("F2 (" + marker + ")"); static Bowl bowl2 = new Bowl (2);} Class Cupboard {Bowl bowl3 = new Bowl (3); static Bowl Bowl4 = new Bowl (4); Cupboard () {print ("cupboard ()"); Bowl4.f1 (2);} void F3 (int marker) {print ("F3 (" + Marker + ")"); static Bowl bowl5 = new Bowl (5);} public class Staticinitialization {public static void main (string[] args) {print ("Creating new Cupboard () in main"); new Cu Pboard ();p rint ("Creating New Cupboard () in main"); new cupboard (); Table.f2 (1); cupboard.f3 (1);} Static Table table = new Table (), static cupboard cupboard = new cupboard ();} /* Output:bowl (1) Bowl (2) Table () F1 (1) Bowl (4) Bowl (5) Bowl (3) cupboard () F1 (2) Creating new Cupboard () in Mainbowl (3) Cupboard () F1 (2) Creating new Cupboard () in Mainbowl (3) cupboard () F1 (2) F2 (1) F3 (1) *///:~
The order of initialization is the first static object, then the non-static object, The above class to run main () (static method) must be loaded into staticinitialization, and then its static domain table and cupboard are initialized. This causes their corresponding classes to also be loaded, and since they all include static bowl objects, bowl is then loaded.


Think in Java (ii): initialization and cleanup

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.