Static keyword and static variable, static code block, static method, static class

1.  Static modified classes can only be internal classes, and ordinary classes cannot be modified with the static keyword. The internal class modified by static is equivalent to an ordinary class, and the access method is (new external class name. internal class method()). As follows:

public class OuterClass {

    public static class InnerClass{

        InnerClass(){

            System.out.println("============= I am an inner class'InnerClass' =============");

        }

    }

}

 

public class TestStaticClass {

    public static void main(String[] args) {

        // No need for a new InnerClass

        new OuterClass.InnerClass();

    }

}

Note: The access method of ordinary inner class is (external class object.new inner class method())

 

2. The access method of static modified static method is  (class name. method name)

3. The difference between static variables and instance variables

 Variables modified by static are static variables. Static variables have only one storage space in memory. Static variables do not belong to an instance object. They are shared by all objects in a class and belong to the class, so they are also called class variables and can be passed directly Refer to the class name.

Instance variables are private to a fixed object, the use of instance variables must first create an instance of the class, and then use this instance to reference.

public class Person {

    int age = 10;

    static  int age1 =11;

}

public class Test {
    public static void main(String[] args) {
        Person person = new Person();
        person.age=11;
        person.age1 = 12;
        Person person1 = new Person();
        
        System.out.println(person.age);
        System.out.println(person.age1);
        System.out.println(person1.age);
        System.out.println(person1.age1);
    }
}

 

 

 Results: 11

12

10

12

 

static

Static variable

Static method

Alias

Class variable

Class method

  level

Same level as the class

Same level as the class

  load

As the class is loaded, it is loaded into the static area of ​​the method area

As the class is loaded, it is loaded into the static area of ​​the method area

  Initial value

Give the system default initial value

Does not give the system default initial value, it will be loaded into the stack for execution when called

  transfer

1. Class name. 2. Object.

1. Class name. 2. Object.

 

Objects generated by the class can share the same static variable

Support overload

 

Static variables cannot be defined in methods (normal methods and construction methods)

Does not support rewriting (rewriting is for objects)

 

 

Static information in Java can only  use static information directly

 

 

But non-static information can  directly use non-static and static

 

5. The format of the static code block is:
public class class name { static { //content of the static code block } Features: When this class is used for the first time, the static code block is executed only once. Static content always takes precedence over non-static content, so static code blocks are executed before construction methods. Typical use of static code blocks: to assign values ​​to static member variables at once.







 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_51980491/article/details/112736632