Java basic knowledge notes 5

final keyword

Overview

The final keyword stands for final and unchangeable.

Four common usages

1. Used to modify a class.

2. Used to modify a method.

3. Used to modify a local variable.

4. Used to modify a member variable.

Used to modify a class

format:

public final class 类名称{
    
    

		//内容

}

Meaning: The current class cannot have any subclasses. (Eunuchs)

Note: If a class is final, then all member methods cannot be overwritten. (Because there is no son)

Used to modify a method

When the final keyword is used to modify a method, the method is the final method, that is, it cannot be overridden.

format:

修饰符  final  返回值类型  方法名称(参数列表){
    
    

//		内容

}

Note: For classes and methods, abstract keywords and final keywords cannot be used at the same time because of contradictions. (Obviously)

Used to modify a local variable

Once final is used to modify a local variable, then this variable cannot be changed.

Assign a value once and remain unchanged for life.

For basic types, immutability means that the data in the variable cannot be changed.

For reference types, immutability means that the address value in the variable cannot be changed.

public class FinalTest {
    
    
    public static void main(String[] args) {
    
    
        int num1 = 10;
        System.out.println(num1);

        num1 = 20;
        System.out.println(num1);

        final int num2 = 100;
        System.out.println(num2);

//        num2 = 200;   错误,无法再次赋值。

        Student stu1 = new Student("大帅");
        System.out.println(stu1);

        Student stu2 = new Student("二帅");
        System.out.println(stu2);

        final Student stu3 = new Student("三帅");
        System.out.println(stu3.getName());

//        stu3  = new Student("小帅");  错误报错,stu3地址不可再改变。
        stu3.setName("小帅");
        System.out.println(stu3.getName());//这个就可以,因为是在引用类型的地址上进行重新赋值,地址并未改变,
    }
}

Used to modify a member variable

For member variables, if you use the final keyword, then this variable is still immutable,

1. Since member variables have default values, they must be manually assigned after being used for final, and default values ​​will no longer be given.

2. For final member variables, either use direct assignment or assignment through construction methods. Choose one of the two.

3. It must be ensured that all overloaded construction methods in the class will eventually assign final member variables.

Permission modifier

There are four permission modifiers in Java

public protected (default) private
The same class (myself) YES YES YES YES
The same package (I live in) YES YES YES NO
Different types of buns (my son) YES YES NO NO
Different packages are not subcategories (strangers) YES NO NO NO

Note: (default) is not the keyword "default", but not written at all.

Inner class

Overview

If a transaction contains another transaction, then this is a class that contains another class.

For example: the relationship between the body and the heart, another example: the relationship between the car and the engine.

classification:

1. Member inner class

2. Local inner classes (including anonymous inner classes)

Member inner class

Define format

修饰符 class 外部类名称 {
    
    
	修饰符 class 内部类名称{
    
    
        //内容
	}
}

Among them, internal use and external use can be accessed at will; external use and internal use require internal objects.

That is, the inner class can directly access the members of the outer class, including private members; if the
outer class wants to access the members of the inner class, an object of the inner class must be established.

Use member inner class

1. Indirect method: In the external and internal methods, the internal class is used, and then the main method just calls the external class.

2. Direct method: external class name. internal class name object name = new external class name (). new internal class name ();

Variable access with the same name of the member inner class

public class Body {
    
    
    int num = 10;
    public class Heart{
    
    
        int num = 20;
        public void dongdong(){
    
    
            int num = 30;
            System.out.println(Body.this.num);
            System.out.println(this.num);
            System.out.println(num);
        }
    }
}

Local inner class

If a class is defined inside a method, then this is a local inner class.

"Partial": Only the current method can use it, and it cannot be used outside this method.

Define format

修饰符  class  外部类名称{
    
    

		修饰符  返回值类型  外部类方法名称(参数列表){
    
    

				class  局部内部类名称{
    
    

						//内容

				}

		}

}

Local variables of local inner classes

Local inner class, if you want to access the local variable of the method, then the local variable must be [effective final].

Remarks: Starting from Java 8+, as long as the local variable facts remain unchanged, the final keyword can be omitted.

the reason:

1. The new object is in the heap memory.

2. Local variables follow the method and are in the stack memory.

3. The stack will be popped immediately after the method runs, and the local variables will disappear immediately.

4. But the new object will continue to exist in the heap memory until the garbage collection disappears.

5. If the object is still alive and want to continue to use local variables, because the previous local variables have disappeared, if you want to use them, you can get the value of the previous local variables in the constant pool, so you need to modify the variables with the final keyword to make It cannot be reassigned, and the original value can always be saved.

Summary permission modifier

public > protected > (default) > private

When defining a class, the permission modifier rules:

1. External class: public / (default)

2. Member inner class: public / protected / (default) / private

3. Local inner class: nothing can be written (different from default)

Anonymous inner class (emphasis)

Overview

If the implementation class of the interface (or a subclass of the parent class) only needs to be used only once,

In this case, you can omit the definition of the class and use [anonymous inner class] instead.

The definition format of anonymous inner class:

Interface name object name = new interface name( ){

​ //Override and rewrite all abstract methods

};

Parsing

Parse the format "new interface name (){ }":

1.new represents the action of creating an object

2. The interface name is which interface the anonymous inner class needs to implement

3. {...} This is the content of the anonymous inner class

note

  1. An anonymous inner class can only be used only once when creating an object. If you want to use it multiple times, and the content of the class is the same, then you must use a separately defined implementation class.
  2. Anonymous objects can only be called once in [calling method]. If you want to call multiple methods on the same object, you must give the object a name.
  3. Anonymous inner classes omit [implementation class/subclass name], but anonymous objects omit [object name]. Anonymous inner classes and anonymous objects are not the same thing. (Basically no problem understanding)

Class as member variable type

See Sum.Demo01

Interface as a member variable type

See Sum.Demo02

Interface as method parameter or return value

public class demo {
    
    

    public static void main(String[] args) {
    
    
        List<String> list = new ArrayList<>();//左边是接口名称,右边是类名称,这就是多态写法
        demo.addList(list);
        System.out.println(list);
    }

    public static List<String> addList(List<String> list){
    
    
        list.add("你好");
        list.add("你好啊");
        list.add("你好啊啊");
        list.add("你好啊啊啊");

        return list;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43215322/article/details/108701459