The difference between Java Foundation-final and Static

Source: Internet
Author: User

Many times it is easy to confuse the static with the final keyword, where static acts on the member variable to indicate that only one copy is saved, and final is used to ensure that the variable is immutable. Look at the following example:

 Public classTest { Public Static voidMain (string[] args) {MyClass MyClass1=NewMyClass (); MyClass MyClass2=NewMyClass ();        System.out.println (MYCLASS1.I);        System.out.println (MYCLASS1.J);        System.out.println (MYCLASS2.I);     System.out.println (MYCLASS2.J); }} classMyClass { Public Final Doublei =Math.random ();  Public Static Doublej =math.random ();}

Running this code will reveal that the two J values are the same for each print, while the value of I is different. From here you can see the difference between final and static variables.

The difference between Java Foundation-final and Static

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.