I heard that wechat search “Java fish” will change strong oh!

This article is in Java Server, which contains my complete series of Java articles, can be read for study or interview

(1) Overview

I started to write my blog before I started to work. Now I find that I have more ideas about what I wrote before, so I have a knowledge rewrite plan. The main purpose is to maintain the knowledge system on Github and make JavaStarter more mature.

In Java, there are many keywords, such as final, static, this, super, public, private, protected, and so on. I have written several articles about these keywords. Today, I will reorganize them all once and for all. It is also a supplement to the previous content.

(2) the final

Final, as a keyword in Java, can be used in three places. Used to modify classes, class properties, and class methods.

Features: Any reference to the final keyword can not be modified!

(1) Modify class: indicates that the class cannot be inherited;

(2) modification method: indicates that the method cannot be overwritten;

(3) Modify variable: indicates that the variable cannot be modified.

Modify variables

Final modifies a variable that it cannot be modified, which means different things for different data types.

First, for the eight basic data types, this value does not change after initialization when final is used.

When final is used to modify data of a reference type, it means that the reference will always point to an address at which the object can be modified.

final User user=new User("javayz".23);
user.setAge(18);
Copy the code

In the above code, the custom User object is modified with final, but the User object can still be modified.

Modification methods

The purpose of the final decorator is to make the method impossible to override, so there’s not much extra here

decorator

When final is used to modify a class, the class cannot be inherited. All member methods of the modified class are implicitly modified as final methods.

(3) of the static

Normally, when we create an object with new, the object’s storage space is allocated, and that’s when the methods in the object can be called. However, there may be a need to access a method or variable in the class without creating an object, in which case the static keyword can be used.

Static methods are called class methods or static methods, and static variables are called class variables or static variables. Classes and methods modified static belong to the class, rather than to an object generated by the class, by class name. Static variable and class name. Static method names access these members.

For example, when writing a utility class, we usually define the method as static instead of creating an object each time we call the utility class:

A code block decorated static is called a static code block, and a static code block is executed only once.

Static: Static: static: static: static

public class Father {
    private int i=test();
    private static int j=method();
    static {
        System.out.print("1");
    }
    Father(){
        System.out.print("2");
    }
    {
        System.out.print("3");
    }
    public int test(a) {
        System.out.print("4");
        return 1;
    }
    public static int method(a){
        System.out.print("5");
        return 1; }}public class Son extends Father{
    private int i=test();
    private static int j=method();
    static {
        System.out.print("6");
    }
    Son(){
        System.out.print("Seven");
    }
    {
        System.out.print("8");
    }
    public int test(a) {
        System.out.print("9");
        return 1;
    }
    public static int method(a){
        System.out.print("10");
        return 1;
    }
    public static void main(String[] args) {
        Son s1=new Son();
        System.out.println();
        Son s2=newSon(); }}Copy the code

The final result is:

Class initialization process:

To initialize a subclass, initialize its parent class; Execute static class variables and static code blocks from top to bottom.

So the first time you create an instance of the Son object, you execute the parent static class variable and the static code block, and then you execute the child class. So it’s 5, 1, 10, 6

Instance initialization

Call Super first; Call non-static instance variables and non-static code blocks to execute from top to bottom; Then call the no-argument constructor; When calling Super, notice whether the object of the parent class is overridden by the subclass, and if overridden, count as overridden by the subclass.

So we call test, which is overridden by the subclass, and we say 9, and then we call nonstatic code, which says 3, and we call the no-argument construct, which says 2, and then the subclass says 987.

(3) of this

The this key refers to an object that refers to the current object itself. Within an object, we can access its properties or methods through this. XXX. When we first started learning Java, the first set method we wrote used the this keyword:

public class User {
    private String name;
    private String address;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address) {
        this.address = address; }}Copy the code

The name variable representing the current object is equal to the name parameter passed in by the setName method.

(4) super

The super keyword can be understood as a pointer to an object of its parent class. You can access the properties and methods of the parent class from the child class, for example:

public class User {
    private String name;
    private String address;

    public void doSomething(a){
        System.out.println("User do something"); }}public class UserDetail extends User {
    void superInfo(a){
        super.doSomething(); }}Copy the code

Since both this and super call properties and methods in the object instance, neither can be used in static methods.

(5) Access modifiers

In Java, access modifiers control the access of each member and method in a class. Public, protected, private, if you do not write access modifiers, it means that it is package access. In some places, it is also called friendly.

Public: visible to all classes. Classes, interfaces, variables, and methods are available.

Protected: Visible to classes and all subclasses within the same package. Variables and methods are available.

Package access (write nothing) : visible within the same package, classes, interfaces, variables, and methods can be used.

Private: visible within the same class. Variables and methods can be used.

(6) Summary

Today’s several knowledge points are the foundation of the foundation, very simple, but if you do not know, the interview will fail. Well, the first knowledge rewrite is over, and I’ll sync it to Github to make Java Collector better over time. I’m Java Fish boy and I’ll see you next time.