Think in Java (ii): initialization and cleanup

Source: Internet
Author: User

1. Differentiate Overloaded methods:
The order of the parameters can be distinguished by two methods, however, in general, do not do this because it makes the code difficult to maintain the overloaded method cannot be distinguished by the return value type: Because if I call F () directly, Java does not know that it should call that
public void F () {
}
public int F () {
return 1;
}

2. You need to use the This keyword only if you need to explicitly indicate a reference to the current object, for example, when you need to return a reference to the current object, it is often written in the return statement:

Because increment () returns a reference to the current object through the This keyword, it is easy to perform 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, and 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 knows only to release memory that is allocated through new, it does not know how to release this "special" memory for that object. To cope with this situation, Java allows you to define a method named Finalize () in the class .

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

The reason to have Finalize () is that it is possible to allocate memory in a manner similar to that in C, rather than the usual practice in Java, where the notion of a "local method" is a way of calling non-Java code in Java.

Neither "garbage collection" nor "finalization method" is guaranteed to occur, and it is not a waste of time if the Java virtual machine is not facing a memory exhaustion situation
To perform garbage collection to recover 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, and they are initialized before any methods (including constructors) are 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, and then the non-static object, the above class to execute main () (static method) must load Staticinitialization, and then its static domain table and cupboard are initialized, which will cause their corresponding class is also loaded , and since they all contain 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.