Talk about something that java in final

What is 1.final

final java is a keyword, a modifier can be used to modify variables, methods, modified class.

What is the use 2.final

  • When the final variables can be modified, its value can not be changed
  • It can not be rewritten when the final modification methods
  • When the final modified class, it can not be inherited.

3.final modified member variable

fianl used when the most common use of modified member variables, member variables divided into static and a regular variable.
For the final modification of variables, not can not be assigned, its value can not be changed, it can be understood only assign a value can be when defining the assignment, it can also be assigned after another definition, but either way can only be assigned once.

(1) modification of static variables

When the modified static variables, you can choose two ways assignment:

  • When defining assignment
  • A static initialization block assignment
final static int a = 6;
final static int b;
static 
{
    b = 6;
}

(2) modifying ordinary member variables

When modifying an ordinary member variable, you can choose one of three ways assignment:

  • When you define the assignment
  • The initialization block assignment
  • Constructor assignment
public class test
{
    int c = 1;
    int d;
    int e;
    {
        d = 2;
    }
    public test()
    {
        e = 3;
    }
}

According to "static" can not access "non-static" rule, that static methods can not access non-static members, static initialization block can not initialize non-static member, ordinary initialization block can not initialize static variables.
However, there is a "bug" is java the method allows access to final members, so ... funny thing happened.

public class test
{
    final int a;
    {
        System.out.println(a);//这里会报错
        printA();
        a = 3;
        printA();
    }
    void printA()
    {
        System.out.println(a);
    }
    public static void main(String[] args) {
        new test();
    }
}

The code above will get an error, because java does not allow final member is not initialized before the visit.

Here Insert Picture Description
After the line that the error code comments above ... actually by the compiler?!

public class test
{
    final int a;
    {
        //System.out.println(a);//这里会报错
        printA();
        a = 3;
        printA();
    }
    void printA()
    {
        System.out.println(a);
    }
    public static void main(String[] args) {
        new test();
    }
}

printA () method in just a wrapper for the output function, actually by the compiler ... there is no interest to look at the results?

Here Insert Picture Description
emmmmmm .... final "Default" is short ..... 0Not in order to access these dishonest final before the final initialization of variables Before using the final variable initialization initialization initialization, the important thing to say three times.

4.final modified local variables

local variables final modification is actually two, one is a modified parameter A is a partially modified method of internal variables

(1) modified parameter

Nothing to say ... is that parameter values ​​can not be changed.

public void f(final int a)
{
    a = 3;//报错.
}

(2) modifying local variable

It can be defined and modified assignment when local variables can also be defined after the assignment (only once).

public void f()
{
    final int  a = 3;
    final int b;
    b = 2;
}

(3) final modified reference variable

Ah ..... This is a special little example of the Talk is cheap. Code.

import java.util.Arrays;

public class test
{
    public static void main(String[] args) {
        final int[] arr = {1,2,3};
        arr[1] = 5;
        Arrays.stream(arr).forEach(System.out::print);
        System.out.println();
        final A a = new A();
        a.setA(9);
        System.out.println(a.getA());
    }
}

class A
{
    private int a = 3;
    public void setA(int a)
    {
        this.a = a;
    }
    public int getA()
    {
        return a;
    }
}

Why ??? final array can be assigned a value fianl object is changed ??? Look at the result:
Here Insert Picture Description
final array actually been changed values ?? final object also changed ??
Actually, because the array is a reference type, final when the modified reference type, can only ensure this variable is always "points" that some memory space saved is just a reference, but the value of that part of the memory space can be changed when modifying the object it is the same reason.

5.final modification methods

The final modification methods can not be overridden, of course, can not "with" private "use" since the private method into a private, equivalent subclass is not visible, do not know the parent class subclass "And this thing." , can be carried out so-called "rewrite" the.

class A
{
    private final void f(){}
}
class B extends A
{
    public final void f(){}//没毛病
}

Since class B f () belong to category B, not inherited from class A over.

6.final modified class

When the final modified class represents the class can not be inherited.

final class A{}
class B extends A{}//出错




It is the author's public number, welcome attention.

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/Blueeeeeeee/p/11740981.html