Initial contact for final keyword in Java

Source: Internet
Author: User


code example

Import java.util.*;

Import static net.mindview.util.print.*;


Class value{

int i;

Public Value (int i) {this.i=i};

}


public class finaldata{

private static Random rand=new random (47);

Private String ID;

Public FinalData (String id) {this.id=id; }

Private final int valueone=9;//privately final member

private static final int value_two=99;//static private final member

public static final int value_three=39;

Private final int i4=rand.nextint (20);

static final int int_5=rand.nextint (20);

Private value V1=new value (11);

Private final value V2=new value (22);

private static final value Val_3=new value (33);

Private final int[] a={1,2,3,4,5,6};

Public String toString () {

Return id+ ":" + "i4=" +i4+ ", int_5=" +int_5;

}

public static void Main (String args[])

{

FinalData fd1=new finaldata ("fd1");

//error:fd1.valueone++;

fd1.v2.i++; //Note: The Member V2 is the final modifier, but V2 represents the reference rather than the object itself

Fd1.v1=new Value (9);

for (int i=0;i<fd1.a.length;i++)

{

fd1.a[i]++;

//error:fd1.v2=new Value (0);

//error:fd1. Val_3=new Value (1);

//error:fd1.a=new int[3];

}

Print (FD1);

Print ("Creating new FinalData");

FinalData fd2=new finaldata ("fd2");

Print (FD1);

Print (FD2);

}

};


Output results

fd1:i4=, int_5= 18

Creating New FinalData

fd1:i4=, int_5= 18

fd2:i4=, int_5= 18


First look at the output: fd1 and fd2 contrast, I4 results have changed, Int_5 has not changed, I4 modifier is private and final; Int_5 modifier is static and final; Because I4 is just a private final member, So the I4 values of the different object references can of course be different, but the value of the i4 of the same object can not change, and int_5 because it is a member of Static, and also has the final keyword modification, so int_5 is a member of the class, and can not be changed;


Let's look at a few more errors; The first error is a mistake that happens to the Fd1.valueone self-addition;

private final int valueone=9; This statement indicates that the Valueone member is a private final member and initialized to 9, so any statements that change it are wrong;

V2 is a reference to the value object and also has a final decoration, which indicates that the object itself is mutable when a reference to the final decorated object is present, but the reference can only point to that object (that is, the reference is immutable);

Val_3 is a member of the private and static and final adornments and is also a reference to an object, so it cannot be changed to point to other objects;

As for arrays, arrays are also references to objects, so you cannot change the objects that you point to;


Summarize:

The final keyword modifies the base data type, the object's reference, the member method, the array, and the class;

1: The modified base data type is, indicating that the variable is immutable;

2: When the object reference is decorated, the reference to that object is immutable and can only point to the object, and the object itself may change;

3: When the Member method is decorated, it means that the method is immutable and will not be overwritten when inheriting;

4: Arrays and object references are similar, and can be regarded as collections of references;

5: Modifier class, indicating that the class does not want to change or can not be inherited;



Initial contact for final keyword 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.