1.final amount of those modified
. A modification class (not used with abstract; final modified classes can not have subclasses)
. B modification methods (static and can be modified based method; access modifiers may be private, but meaningless; Final methods can not override)
C. Examples of variables (obtained can not be re-assigned after the initial values may give the definition of the assignment , also initialization block assignment , also be configured number assignment . can not assigned , the assignment can not be re-assigned)
D. class variable (obtained can not be re-assigned after the initial values may give the definition of the assignment , also initialization block assignment . can not assigned , the assignment can not be re-assigned)
e. parameter (which is no longer in the assignment method)
f. Local variables (program does not assign an initial value to a local variable, final modified local variables give the definition of assignments , or the definition of the assignment )
g. modified reference variable, the variable address reference only guarantee constant, within the arrangement can be changed
You can not directly access a given initial 2.final yet, but the method can be used to access
public class FinalErrorTest { // define a final modification of the instance variables // will not be on the final member variables default initialization final int Age; { // Age is not initialized, so here code will cause an error. // System.out.println (Age); printAge (); Age =. 6 ; System.out.println (Age); } public void printAge () { System.out.println (Age); } public static void main ( String [] args) { new new FinalErrorTest (); } }
3. Whether the final modification of class variables, instance variables or local variables, if direct assignment when this variable is defined, and the value can be determined at compile time down , then the final modification is a direct variable amounts (macro substitution)
If the assignment is an expression, the expression is the basic arithmetic expression or a character string is connected, this will also be the final variable amount of a direct
If there is no final modification
public class FinalReplaceTest { public static void main(String[] args) { String a="testa"; System.out.println(a); } }
Use javap -c view decompile results
final modification, the definition of the assignment, and the value can be determined at compile time
public class FinalReplaceTest { public static void main(String[] args) { final String a="testa"; System.out.println(a); } }
Use javap -c view decompile results
The following macro will be replaced
public class FinalReplaceTest { public static void main(String[] args) { final int a = 5 + 2; final double b = 1.2 / 3; final String str = "疯狂" + "Java"; final String book = "疯狂Java讲义:" + 99.0; } }
4. immutable class , create an instance of the class means, an example of instance variables can not be changed (in the packaging and 8 immutable class java.lang.String)
. A private instance variables and modified with the final (final modification, can not be modified; the general private instance variables, encapsulation)
b. Provide substituting argument constructor to initialize instance variables (one way to initialize instance variables)
c. provide for the variable getter method does not provide a setter method (method not modify)
e., if necessary, rewrite hasCode () and equals () method
The cache immutable class instance
topic
1. The judgment of those who place lettered assignment is correct, those things wrong
public class FinalLocalVariableTest { public void test(final int a) { a = 5; //a } public static void main(String[] args) { final String str = "hello"; //b str = "Java";//c final double d;//d d = 5.6;//e d = 3.4;//f } }
2. The reference variable assignment
class the Person { Private int age; public the Person () {} // have constructor parameters public the Person ( int age) { the this .age = age; } // omitted age setter and getter methods // age setter and getter method public void the setAge ( int Age) { the this .age = Age; } public int getAge () { return the this .age; } } public classFinalReferenceTest { public static void main (String [] args) { // Final modified array variables, iArr is a reference variable Final int [] = {IARR. 5,. 6, 12 is,. 9 }; System.out.println (of Arrays.toString (IARR)); // array elements are sorted, legal Arrays.sort (IARR); System.out.println (of Arrays.toString (IARR)); // array element assignment legitimate iArr [2] = -8 ; System.out.println (Arrays.toString (IARR)); // The following statement reassigned iArr, illegal // IARR = null; // Final modifications Person variable, p is a reference variable Final Person the p-=new new Person (45 ); // change Person object instance variables age, legal p.setAge (23 ); System.out.println (p.getAge ()); // the following statement on p reassigned illegal // p null =; } }
3 write the following program running results
public class FinalReplaceTest { public static void main(String[] args) { String a ="疯狂JAVA"; String b="疯狂"; String c="JAVA"; String d = b+c; System.out.println(a == d); final String e="疯狂"; final String f="JAVA"; String g = e+f; System.out.println(a ==g); System.out.println(b==e); } }
4. Determination of the program given below
public class FinalMethodTest { public final void test(){} } class Sub extends FinalMethodTest{ public void test(){} }
The program determines whether or not given below
public class FinalMethodTest { private final void test(){} } class Sub extends FinalMethodTest{ public void test(){} }
6. Write the presence of a two instance variables (respectively String firstName, String lastName), the class name MyName immutable class
7. Write a presence of two instance variables (respectively Integer age, MyName myname), the class name of the class Person immutable
8. The write buffer portion immutable class instance of a
1. error correct correct correct wrong wrong
3.false true true
4. will complain
5. without error