The difference between static,final and static final in Java

Source: Internet
Author: User

Package Com.victor.test;import Java.util.random;public class Staticandfinaltest {private static random Rand = new Random ( ;p rivate static int statica = Rand.nextint (+);p rivate final int finalb = Rand.nextint (+);p rivate static final int s Taticfinalc = Rand.nextint (+);p ublic static void Main (string[] args) {    staticandfinaltest saft1 = new Staticandfina Ltest ();    System.err.println ("Saft1.statica:" + saft1.statica);    System.err.println ("Saft1.finalb:" + saft1.finalb);    System.err.println ("Saft1.staticfinalc:" + saft1.staticfinalc);    System.err.println ("---------------------------------");    Staticandfinaltest saft2 = new Staticandfinaltest ();    System.err.println ("Saft1.statica:" + saft2.statica);    System.err.println ("Saft1.finalb:" + saft2.finalb);    System.err.println ("Saft1.staticfinalc:" + Saft2.staticfinalc);}

}

The result of the operation is:

Saft1.statica:58saft1.finalb:93saft1.staticfinalc:55---------------------------------Saft1.statica: 58saft1.finalb:61saft1.staticfinalc:55

This is because it is static and is initialized at mount time instead of every time a new object is created.

The difference between static,final and static final 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.