How to use final, finally, finalized in Java

Source: Internet
Author: User

First you need to understand that final and finally are the keywords, and Finalize is a method.

1. Final keyword

Final can modify classes, methods, variables,

    • A decorated class indicates that a class cannot be inherited
    • The modifier means that this method cannot be overridden (overwritten) but can be overloaded
    • A modifier variable indicates that the variable is immutable (the reference is immutable-that is, you cannot re-point to another object, but the reference content can change), and static final is often used as a constant processing.
 PackageCn.qlq.fina;/*** Final keyword use *@authorAdministrator **/ Public classTESTCLA {Private Static Final intA = 1; Private Static FinalUser U =NewUser ();;  Public Static voidMain (string[] args) {//a = 2;//final modified variables are immutable (reference immutable)//u=new User ();//references are immutable and cannot point to other objects, but reference content can changeU.setusername ("Sssssss"); U.setusername ("XXXXXXX"); }         Public voidTest1 (Final intx) {//x=1;//The final modified variable is immutable, and the parameters on the method cannot be changed.    }}

Use of 2.finally:---typically used in try: Catch.. Then close some IO streams and so on
    1. The try statement was not executed, as it was returned before the try statement, so that the finally statement would not be executed, and therefore the necessary rather than sufficient condition for the finally statement to be executed is that the corresponding try statement must be executed.

    2. If you execute the system.exit (0) statement in a try code block, the Java Virtual machine JVM is terminated, and therefore the finally statement is not executed.

    3. A finally block statement that executes after the return statement in a try or catch is executed and the modified statement in the finally may or may not affect the return value that has been determined in the try or catch, and if the return value type is the address type, affects , the value type is not affected. If there is a return statement in Finally, the return statement in the try or catch is overwritten directly.

Test code:

 Packagecn.qlq.test;/** *  * @authorAdministrator **/ Public classMyTest { Public Static Final inti = 6; @SuppressWarnings ("Finally")     Public Static Longtest1 () {String Val= "5d"; LongValue = 5; Try{Value=Long.parselong (Val); } Catch(NumberFormatException e) {System.out.println ("Catch ..."); returnvalue++; } finally{System.out.println ("Finally ..."); Value--; returnvalue; }    }         Public Static voidMain (string[] args) {System.out.println (Mytest.test1 ()); }}

Results:

Catch ... finally ... 5

3.finalize () Use of the Destructor method

Finalize () calls this method of the object when the JVM reclaims the object, and handles some things when it is garbage collected.

 Packagecn.qlq.test;/*** * Test the Finalize () method, the method called when the object dies, similar to the C + + destructor *@authorAdministrator **/ Public classMyTest { Public Static Final inti = 6; @Overrideprotected voidFinalize ()throwsthrowable {System.out.println ("Object to Die"); } @SuppressWarnings ("Finally")     Public Longtest1 () {String Val= "5d"; LongValue = 5; Try{Value=Long.parselong (Val); } Catch(NumberFormatException e) {System.out.println ("Catch ..."); returnvalue++; }finally{System.out.println ("Finally ..."); Value--; returnvalue; }    }     Public Static voidMain (string[] args) {MyTest my=NewMyTest ();        System.out.println (My.test1 ()); My=NULL;//clears the object's reference so that it is garbage collectedSystem.GC ();//Call GC Recycle garbage    }}

Results:

Catch...
Finally ...
5
Object to die

How to use final, finally, finalized in Java

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.