On the final and static keywords

table of Contents

 

final keyword

1.1 final modified class

1.2 final modification methods

1.3 final modification of local variables

1.4 final member variables modified class

The static keyword

         2.1 static modification of the object-specific data

2.2 static precautions _ still can not directly call a non-static

2.3 static static usage scenarios

2.4 object static call

2.5 define static constants


final keyword

 

  • Appears inherited improve the reusability of code, and to facilitate the development. But will also have problems, some classes after describing finished, I do not want to be inherited, or part of some class method function is fixed, do not want subclasses override. But when the subclass inherits these special classes, a method which can be rewritten, then how to solve it? To solve the above problems, it is necessary to use a keyword final, final mean for the final, immutable
  • a final modifier, it can be used to modify class member variable classes, methods, local variables
  • The Java String class and eight data wrapper types (Integer ...) are modified final class. Protection

1.1 final modified class

Features final modified class

  • final modified class can not be inherited, but can inherit from other classes
  • final modified class all methods are implicitly plus final keyword (does not include the member variables)

Examples

class A {}

//被 final 修饰的类 
final class B extend A {}//可以继承A类
class C extends B {}//不能够继承B类

1.2 final modification methods

Features final modification methods

  • final modified method can not be rewritten (overwritten)! However, the parent class is not final modification method, can be added after the final subclass overrides
  • final modification of the method commonly used in the template methods - conservation

Examples

class A {
    //被 final 修饰的方法不能够被重写,但可以继承使用
    public final void method1(){}
    public void method2(){}
}
class B extends A{
    //重写method2方法;可以选择性添加 final 修饰符
    public final void method2(){}
}

1.3 final modification of local variables

final modification of the basic data types of variables

  • The final modification of the basic data types of variables are called constants, these variables can only be assigned once

Examples

final int a = 10;
a = 20;//赋值出错;被 final 修饰的局部变量只能被赋值一次

final modified reference data types

  • An object reference type variable value address value, the address value can not be changed, but the object attribute value may be modified within the address

For example

final Person person1 = new Person();
Person person2 = new Person();
person1 = person2;//final 修饰的变量person1,所保存的地址不呢个改变
person1.name = "kobe";// 可以更改person1中对象的属性值

1.4 final member variables modified class

Modified class member variables final

Before the assignment need to create an object when modified class member variable, otherwise an error

Member variables are initialized:

  • Direct Initialization: public static int a = 5; class generally used constants final + static, global variables described
  • Initialization code blocks {a = 5;}
  • Constructor to initialize

Examples

class Person{
    //直接初始化
    final String name = "kobe";
    {
        //代码块初始化
        this.name = "gigi";
    }
    public Person(String name) {
        //构造方法初始化
        this.name = name;
    }
}

The static keyword

 

  • When a class definition, will have a corresponding class attributes and methods. The properties and methods that are created by this class of objects called
  • When you call an object's method, this method does not have access to specific data object, the method creates an object somewhat redundant
  • But do not create objects, methods, and can not call, then we can be achieved through the static keyword. It is a static modifier static, generally used to modify the members of the class

2.1 static modification of the object-specific data

Feature

Modified static member variables belong to the class, not part of a class of objects. (In other words, when multiple objects modified to access or modify static member variables, one of the objects will be static member variables values ​​were modified, static member variables in other objects of value change with that multiple objects share the same static Member variables)

Code demonstrates

class Person{

    static int a = 10;

}
public class Test {
    public static void main(String[] args) {
        Person person1 = new Person();
        Person person2 = new Person();
        person1.a = 100;
        System.out.println(person1.a);//结果为100
        System.out.println(person2.a);//结果为100
    }
}

2.2 static precautions _ still can not directly call a non-static

Precautions

  • The modified static members and suggested that direct access by the class name

Format access static members

  • Class name. Static data variable name
  • The class name. Static member method name (parameters)

The format is not recommended to visit

  • The object name static member variable names - do not recommend using this mode, there will be a warning
  • The object name static member method name (parameter) - is not recommended to use this way, there will be a warning

Code demonstrates

class Person{
    final String name = "kobe";
    final static int a = 10;
    public static void method(){
        System.out.println("method");
    }
}
public class Test {
    public static void main(String[] args) {
        Person person = new Person();

        System.out.println(Person.a);//建议访问格式
        System.out.println(person.a);//警告

        Person.method();//建议访问格式
        person.method();//警告
    }
}

Note : When using an object reference to call the static method person.method (); the compiler will default to Person.mothed (); therefore person what does not matter, even if it is null also still be able to run properly

​​​​​​​

2.3 static static usage scenarios

scenes to be used

It can be modified static member variables and member properties

When using a modified static member variables?

  • When members add static modification, all objects that are members of the class to share. Generally, we put the common data is defined as static variables

When using a modified static member methods?

  • Static methods can access only static members, if a static method reference other static members, then this method will need to be declared as static methods

​​​​​​​

2.4 object static call

Static Invocation object

  • In the multi-state, non-static compiler to see the parent class, run to see subclass, the father did not fail to compile
  • Polymorphic Static method, the compiler see the parent, the parent class operation still do, because the static and object does not matter, is a static binding

Examples

class Fu{
    static void show(){
        System.out.println("Fu:show");
    }
}
class Zi extends Fu{

    public Zi() {
        super();
    }

    public static void main(String[] args) {
        Fu fu = new Zi();
        fu.show();//父类的引用和父类的方法绑定,和对象无关,不会执行子类的同名方法
    }

}
//执行结果:Fu:show

2.5 define static constants

Static const

When we need to define a static constant in a class, it is usually to modify the variables used to achieve a static public static final constants. Habit variable name in uppercase letters

Definition Format

public static final variable name = value data type;

Examples

class A {
    public static final String NAME = "静态常量";
    public static void method(){
        System.out.println("静态方法method");
    }
}
//当我们使用类的静态成员时 不需要new对象,直接使用类名调用即可

System.out.println(A.NAME);//打印静态常量
A.method();//静态方法method

note

Each member variable interface default public static final modification

All member variables in the interface has a static constant, because the interface has no constructor, it is necessary to display assignment. You can directly access the interface name

Examples

interface Inter{
    public static final int NUMBER = 100;
}
//访问接口中的静态变量
Inter.NUMBER               100

 

 

 

 

Published 20 original articles · won praise 9 · views 896

Guess you like

Origin blog.csdn.net/weixin_44915811/article/details/104637412