Final, finalize, finally

Source: Internet
Author: User
final、finalize、finally
Final: Used to declare attributes. Methods and classes indicate that attributes are unchangeable, methods cannot be overwritten, and classes cannot be inherited. Note: The member variables declared by final must be assigned values at the same time. The local variables declared by final are the same as ordinary local variables. You can declare them without assigning the initial values, but you must assign values before using them. Finalize is a method of the object class. This method is called when the garbage collector is executed. It can overwrite this method to collect other resources during garbage collection, for example, close a file. Finally: return, continue, and break cannot block the execution of finally statement blocks. The following is a return statement in try, so will the code in finally {} following this try be executed, when will it be executed, before or after return? Answer: before return, but to be more detailed, it is executed in the center of return (you can set a breakpoint experiment at the Return Statement ). Eg1: Code: public class test {public static void main (string [] ARGs) {system. out. println (test. test ();} static int test () {int x = 1; try {return X;} finally {++ X; system. out. println (x) ;}}result: 21 analysis: the process in which the main function calls the subfunction and obtains the result is like preparing an empty jar for the main function. When the subfunction returns the result, put the result in the jar first, and then return the program logic to the main function. The so-called Program Logic return refers to the sub-function. If I don't run it, you can continue to run the main function. There is no result. The result is put into the jar before it is said! Eg2: Code: Public final class finallytest {public static returnclass testreturn () {try {return New returnclass ();} catch (exception e) {e. printstacktrace ();} finally {system. out. println ("Finally statement executed");} return NULL;} public static void main (string [] ARGs) {finallytest. testreturn () ;}} class returnclass {public returnclass () {system. out. println ("Return Statement executed");} result: The Return Statement is executed and finally statement analysis is performed. From the output result, return The statement seems to have been executed before the finally statement block. Is that true? Let's take a look at what is the role of the Return Statement? Is to exit the current method and return the value or object. If the finally statement block is executed after the return statement, the Return Statement has exited the current method after it is executed. How can the finally statement block be executed? Therefore, the correct execution sequence should be like this: when the compiler compiles return newreturnclass ();, it is divided into two steps: New returnclass () and return, the statement of the previous object is executed before the finally statement block, and the Return Statement is executed after the finally statement block, that is to say, the finally statement block is executed before the program exits. Similarly, the finally statement block is executed before the loop is skipped (CONTINUE) and interrupted (break.
 

Final, finalize, finally

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.