The difference between final and static final-modified constants in Java

Source: Internet
Author: User
Tags rand random seed

Meow Meow Drive, Novice Road, a lot of care. Any errors please indicate in the comment area.

........................................... I am the dividing line of evil (??ω??)? .........................................

Final: Used to modify classes, variables, methods:

The final decorated class cannot have subclasses, nor can it be inherited.

The final modified variable is a constant and cannot be re-assigned.

The final decorated method cannot be overridden

The difference between the final modified constant ① and the static final-modified constant ② is now explained.

For example:

①final long Current_time=system.currentmillis ();

②static final long Current_time=system.currentmillis ();

Do not use static adornments:

If you assign a fixed value directly to a constant, the value of the constant is fixed, that is, the values in multiple objects are the same. If you are building a constant with a function or an object, the initialization value for its constants may be different each time you create an object. Therefore, the definition of a Java constant that uses only final is not constant.

Use the static modifier:

A storage space is created for this variable in memory before the object is created, and the storage space for this variable is shared if the static variable is needed for later creation.

If you're not sure, here's the code example (reprinted from http://advance0683.iteye.com/blog/1107732)

1 ImportJava.util.Random;2 3 //This example is to illustrate the difference between static final and final4  Public classStaticandfinaltest {5     6     Private StaticRandom Rand =NewRandom (47);//47 as a random seed, the result is a random number. 7     8     Private Final intA = Rand.nextint (20);9     Ten     Private Static Final intB = Rand.nextint (20); One  A      Public Static voidMain (string[] args) { -Staticandfinaltest SF =Newstaticandfinaltest (); -System.out.println ("SF:" + "a=" +SF.A); theSystem.out.println ("SF:" + "b=" +SF. B); -System.out.println ("------------------------------"); -Staticandfinaltest SF1 =Newstaticandfinaltest (); -System.out.println ("SF1:" + "a=" +sf1.a); +System.out.println ("SF1:" + "b=" +SF1. B); -     } +  A}

Here is the result of the code:

Sf:a=15

Sf:b=18

--------

Sf1:a=13

sf1:b=18

As you can see, in SF and SF1 objects, the value of a is not unique, but the value of B is not changed by creating a Sf1 object.

This is consistent with the above-mentioned.

(??ω??)?

The difference between final and static final-modified constants in Java

Related Article

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.