java foundation (1)

java keyword

final

data

Basic data types: make the value unchanged
Reference data types: the reference is unchanged, that is, it cannot refer to other objects. But the referenced object can be modified

method

Declared methods cannot be overridden by subclasses.
Private methods are implicitly designated as final. If a method defined in a subclass has the same signature as a private method in the base class, the subclass method does not override the base class method, but defines a method in the subclass. new method.

kind

Classes cannot be inherited

static

1. Static variables

Static variables exist only once in memory and are only assigned once when the class is initialized.

  • Static variables: All instances of a class share static variables, which can be accessed directly through the class name;
  • Instance variables: Every time an instance is created, an instance variable is created, which lives and dies with the instance.
public class A {
     private int x; // instance variable public static int y; // static variable}

2. Static methods

The static method exists when the class is loaded, it does not depend on any instance, so the static method must be implemented, that is to say it cannot be an abstract method (abstract).

3. Static block

Static statement blocks are run once when the class is initialized.

4. Static inner class

A type of inner class, a static inner class does not depend on the outer class, and cannot access the non-static variables and methods of the outer class.

5. Static import package

import static com.xxx.ClassName.*

No need to specify the ClassName when using static variables and methods, which simplifies the code, but greatly reduces the readability.

6. Variable assignment order

The assignment of static variables and the execution of static statement blocks take precedence over the assignment of instance variables and the execution of ordinary statement blocks. The assignment of static variables and the execution of static statement blocks are executed first depending on their order in the code.

public static String staticField = "static variable ";
static {
     System .out .println( "static statement block "); }
public String field = "instance variable ";
{
     System .out .println( "normal block "); }

Run the constructor at the end

public InitialOrderTest() {
     System .out .println( "Constructor "); }

In the presence of inheritance, the initialization sequence is:

  1. Parent class (static variable, static statement block)
  2. Subclasses (static variables, static statement blocks)
  3. Parent class (instance variables, normal statement blocks)
  4. parent class (constructor)
  5. Subclasses (instance variables, normal statement blocks)
  6. subclass (constructor)
Content from https://github.com/CyC2018/Interview-Notebook/blob/master/notes/Java/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324596951&siteId=291194637