Case analysis of final modifiers in Java _java

Source: Internet
Author: User

Final modifier:

The final modifier member variable must have a programmer display specifying the initial value.
Class: The initial value must be specified in a static initialization block or when the field is declared.
Instance field: The initial value must be declared in a field or constructor in a non-static initial block.

Final local variable: initialization must be shown by the programmer.

What is the difference between the base variable and the reference type variable of the final modification?

Final modified basic variable: cannot be assigned a value to the base variable.
Final modified reference variable: only guarantee that the address referenced by this reference type will not change, i.e.
Always refer to the same object, but this object can be changed completely.

Copy Code code as follows:

/**

*/
Import java.util.*;
public class Demo5
{
public static void Main (string[] args)
{
Final B = new B (22);
B.test ();
Legally change the value, but still point to the same reference
B.setage (20);
System.out.println (B.getage ());
Illegal
b = null;
B.test2 ();
}
}
/**
FIANL Modifier member Variable
*/
Class A
{
Legal
Final int a = 10;
constructor or initialization block to specify the initial value
Final String str;
final int C;
Final static double D;
{
str = "Hello";
Illegal
A = 100;
}
Static
{
d = 100;
}

The constructor can specify an initial value for the field that is not specified in the initialization block
Public A ()
{
Illegal
str = "DDD";
c = 1000;
}
Public double changfinal ()
{
Cannot specify initial value for final in normal method
return d = 1000.90;
return 0;
}
}
/**
Fianl decorated Array objects
*/
Class B
{
private int age;
Public B ()
{

}

Public B (int age)
{
This.age = age;
}

public void Setage (int age)
{
This.age = age;
}

public int Getage ()
{
return this.age;
}

public void Test ()
{
Final int[] arr={23,434,56,898};
System.out.println (arrays.tostring (arr));
Arrays.sort (arr);
System.out.println (arrays.tostring (arr));
ARR[2] = 200;
System.out.println (arrays.tostring (arr));
The following is an illegal arr assignment
arr = null;
}

/**
Deepen the understanding of final
*/
public void Test2 ()
{
String str1 = "bright Future";
Direct reference to the "bright future" in a constant pool
String str2 = "Beautiful" + "future";
True
System.out.println (str1 = = str2);

String S1 = "Beautiful";
String s2 = "Future";
String s3 = S1+s2;
False S1 s2 Only variables cannot be determined at compile time
If you want to determine at compile time, use final modifier s1 s2
System.out.println (str1 = = S3);
}
}

Do the small partners have a knowledge of the final modifier in Java, I believe the comments have been explained to everyone clearly, I will not be long-winded here

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.