Class variables/methods, main syntax, code blocks

1. Class variables and methods

Mind map overview:

1.1 Class variables (static variables)

1.What are class variables/methods?

–Add the static keyword to modify the member properties or member methods in the class. Class variables/methods are also called static variables/methods. Static variables/methods are shared by all objects of the class itself.

2. A small case to quickly understand class variables/methods

public class Test {
    public static void main(String[] args) {
        //Static properties can be accessed directly using class names or object names, but access permissions are respected
        System.out.println(Animal.getAge());//18
        Animal jack = new Animal("jack");
        System.out.println(jack.getAge());//18
    }
}

class Animal {
    //Member variables
    private String name;
    //class variable
    private static int age;

    public Animal(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

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

    //static method
    public static int getAge() {
        age = 18;
        return age;
    }
}

3. Memory layout of class variables

The figure shows:

1) Static variables (static variables) are shared by all objects of the class, and these objects all belong to the same class

2) Static variables are generated when the class is loaded

4. Definition and access of static variables

1) Definition of static variables

i. Access modifier static data type variable name; (recommended)

ii.static access modifier data type variable name;

2) Access to static variables

i.Class name.Static variable name (recommended)

ii. Object name. Static variable name

public class Test {
    public static void main(String[] args) {
        //Two ways to access static variables
        //Class name.Static variable name
        //Static variables are created as the class is loaded, so there is no need to use created objects to access them.
        System.out.println("Name:" + Animal.name + "Age:" + Animal.age);

        //Object name. Static variable name
        Animal animal = new Animal();
        System.out.println("Name:" + animal.name + "Age:" + animal.age);
    }
}

class Animal {
    //Two ways to define static variables
    //Static variables also need to comply with access permissions
    public static String name = "大黄"; //This method is recommended
    static public int age = 18;
}

5. Detailed discussion of static variables

1) When do you need to use static variables?

When all objects need to share a certain variable, we can set this variable as a static variable

2) The difference between static variables and member properties

i. Static variables are shared by all objects of the same class

ii. Member attributes are exclusive to each object, and member attributes between objects are independent.

3) Static variables do not need to be accessed using object names, that is, class variables can be accessed without creating an object.

4) The life cycle of static variables is generated when the class is loaded and dies when the class is destroyed.

public class Test {
    public static void main(String[] args) {
        //Member properties are independent between each object
        Person jack = new Person("jack", 18);
        Person marry = new Person("marry", 19);

        //Static variable assignment
        marry.setSalary(10000);
        System.out.println(jack.getSalary());//10000
        System.out.println(marry.getSalary());//10000
    }
}

class Person {
    //Member properties
    private String name;
    private int age;

    //static variable
    public static char gender = 'male';
    //If the static variable is not assigned a value, a set method should be provided for assignment.
    private static double salary;

    //Constructor
    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    //If the static variable is private, then we provide get/set for assignment and value acquisition
    public void setSalary(double salary) {
        //In order to distinguish the two salaries, the static variable uses the class name.salary
        Person.salary = salary;
    }

    public double getSalary() {
        return Person.salary;
    }
}

1.2 Class method (static method)

1. Definition and access of static methods

1) Definition of static method

i. Access modifier static Returned data type Method name (parameter list) {}; (recommended)

ii.static access modifier returned data type method name (parameter list) {};

2) Access methods of static methods

i. Use class name.static method name to access

ii. Use object.static method name

public class Test {
    public static void main(String[] args) {
        //Access form of static method
        //Form 1: class name.static method name
        Person.say();
        Person.hi();
        
        //Form 2: object name.static method name
        Person person = new Person();
        person.say();
        person.hi();
    }
}

class Person {
    //How to define static methods
    //Definition 1: Access modifier static returned data type method name (formal parameter list) {}
    public static void say() {
        System.out.println("Hello,bro");
    }

    //Definition 2: static access modifier returned data type method name (formal parameter list) {}
    static public void hi() {
        System.out.println("HI,bro");
    }
}

2. Usage scenarios of static methods

–When the method does not involve any members related to the object, it can be set as a static method

Note: The methods in the tool class are all static methods, which improves development efficiency.

3. Detailed discussion of static methods

1) The loading of static methods and the loading of ordinary member methods are loaded as the class is loaded, and the structural information is stored in the method area.

2) The this and super keywords cannot be used in static methods. Why?

Because static methods can be accessed without creating an object, while the this and super keywords require objects to be used

public class Test {
    public static void main(String[] args) {
        Stu.say();
    }
}

class Person {
    public int age = 18;
}

class Stu extends Person {
    //Member properties
    private String name = "jack";

    //Constructor
    publicStu() {
    }

    //This and super keywords cannot be used in static methods
    public static void say() {
        //System.out.println("name=" + this.name);
        //System.out.println(super.age);
        System.out.println("Hello,bro");
    }
}

3) Access rules for ordinary member methods and static methods

i. Ordinary methods can access static properties and ordinary member properties

ii. Static methods can only access static properties

public class Test {
    public static void main(String[] args) {
        Person person = new Person();
        person.bye();
        Person.hi();
    }
}

class Person {
    //Note: Methods and variables are called properties
    //Ordinary member variables
    private String name;
    private int age;
    //static variable
    private static double salary = 10000.0;

    //Ordinary member methods
    public void say() {
        System.out.println("Hello");
    }

    //Ordinary member methods can access static properties and ordinary member properties
    public void bye() {
        //Common member attributes
        say();//ordinary method
        System.out.println(name);//Ordinary members
        System.out.println(age);

        //static properties
        hi();//static method
        System.out.println(Person.salary);//Static variables
    }

    //Static methods can only access static properties
    public static void hi() {
        ok();//static method
        System.out.println(Person.salary);//Static variables

        //System.out.println(age);Error
    }

    public static void ok() {
        System.out.println("Ok~");
    }
}

2.main syntax

Mind map overview:

1.What is the main method?

–The main method is the main method of the program. A program can only have one main method. The main method is the entry point of the program.

2.The role of the main method

–The main method is for the Java virtual machine to call the program. The main method serves as an entrance.

3. Detailed syntax of main method

public static void main(String[] args) {}

explain:

1) Why do we need public access modifier?

–Because the Java virtual machine needs to call the main method outside the class, the access permission of the main method must be public

2) Why is the static keyword needed?

–Because after using the static keyword, we can access the main method without creating an object

3) Why do you need to use the void keyword?

–Because we do not require the main method to return a value to us, which is void

4) What is the role of String[] args?

–String[] args receives the string in the command line

4. Small tips

1) In the main method, you can only directly call the static properties of this class, and you cannot directly call the non-static methods of this class.

2) Non-static properties that cannot be called directly need to be accessed after creating the object

public class Test {
    private int age = 18;
    public static int sal = 10000;
    
    public static void main(String[] args) {
        //Static properties can be accessed directly in the main method
        System.out.println(sal);
        //In the main method, you need to create the object before accessing the member properties.
        Test test = new Test();
        System.out.println(test.age);
    }
}

Three. Code blocks

Mind map overview:

1. What is a code block?

–The code block is a member of the class and is part of the class. Similar to a method, the code block uses {} to enclose the logical statement; the code block has no method name, return parameters, parameter list, and only the method body, which cannot be passed through the class. Explicit call, can only be used implicitly

2. Code block syntax

grammar:

(modifier) {

code

};

Note:

1) The modifier is only static.

2) Code blocks are divided into static code blocks and ordinary code blocks. Static code blocks are modified with static

3) Any logical statement can be written within the code block

4); number can be written or not

public class Test {
    public static void main(String[] args) {
    }
}

class Person {
    //Static code block
    static {
        System.out.println("static code block");
    }
    
    //Normal code block
    {
        System.out.println("Normal code block");
    }
}

3. Advantages of code blocks

1) Equivalent to another form of constructor (supplementary mechanism to the constructor), which can perform initialization operations

2) If there are the same statements in multiple constructors, they can be extracted into the code block for initialization, which can achieve code reusability.

4. A small case to quickly understand the code block

public class Test {
    public static void main(String[] args) {
        Person person = new Person("jack", 18);
    }
}

class Person {
    //Member variables
    private String name;
    private int age;

    //Static code block
    static {
        System.out.println("static code block");//static code block
    }

    //Normal code block
    {
        System.out.println("Ordinary code block");//Ordinary code block
    }

    //Constructor
    public Person(String name, int age) {
        System.out.println("Constructor initialization~");//Constructor initialization~
        this.name = name;
        this.age = age;
    }

    //method
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

5. Detailed discussion of code blocks

1) The code block modified by static is called a static code block. Its function is to initialize the class. It is executed as the class is loaded and will only be executed once.

2) The execution of the instance code block will be executed as the object is created. If there are several objects, the instance code block will be executed several times.

public class Test {
    public static void main(String[] args) {
        //Four objects are created: the static code block will only be executed once, and the ordinary code block will be executed several times if there are several objects.
        //Output static code block once and ordinary code block four times
        Person p = new Person();
        Person p1 = new Person();
        Person p2 = new Person();
        Person p3 = new Person();
    }
}

class Person {
    //Static code block
    static {
        System.out.println("static code block");//static code block
    }

    //Normal code block
    {
        System.out.println("Ordinary code block");//Ordinary code block
    }
}

3)When is the class loaded?

i. Load the class when creating an object instance

ii. Create a subclass object instance, the parent class will also be loaded, load the parent class first

iii. Class loading when using static members

public class Test {
    public static void main(String[] args) {
        //Note: Each test case is tested separately
        //Load the class when creating an object instance
        //Person person = new Person("jack",18);
        //Output: Static code block of the parent class~ Ordinary code block of the parent class~ Constructor of the parent class~

        //Create a subclass object instance, first perform static code blocks of the parent class and subclass, then load the parent class, and then load the parent class
        //Stu stu = new Stu("marry",16);
        //Output: Static code block of the parent class~ Static code block of the subclass~ Ordinary code block of the parent class~ Constructor of the parent class~ Ordinary code block of the subclass~ Constructor of the subclass~

        //Class loading when using static members
        System.out.println(Stu.gender);
        //Output: Static code block of the parent class~ Static code block of the subclass~ Male
    }
}

class Person {
    //Member properties
    private String name;
    private int age;

    //Static code block
    static {
        System.out.println("Static code block of parent class~");
    }

    //Normal code block
    {
        System.out.println("Normal code block of parent class~");
    }

    //Constructor
    public Person(String name, int age) {
        System.out.println("Constructor of parent class~");
        this.name = name;
        this.age = age;
    }

    //method
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

class Stu extends Person {
    //static variable
    public static char gender = 'male';

    public Stu(String name, int age) {
        super(name, age);
        System.out.println("Constructor of subclass~");
    }

    //Static code block
    static {
        System.out.println("Static code block of subclass~");
    }

    //Normal code block
    {
        System.out.println("Ordinary code block of subclass~");
    }
}

4) In a single class, when creating an object, the calling sequence of the class is as follows:

i. Call static member initialization first. Static code blocks and static properties have the same priority. The one defined first is called first.

ii. Then call the ordinary code block and ordinary member attribute initialization. The priorities are the same. The one defined first is called first.

iii.Finally call the constructor

public class Test {
    public static void main(String[] args) {
        Person jack = new Person("jack", 18);
        //Output: Static code block of the parent class~ Ordinary code block of the parent class~ Constructor of the parent class~
    }
}

class Person {
    //1) Call static member initialization first in the class. Static code blocks and static properties have the same priority. The one defined first is called first.
    //static members
    public static char gender = 'male';

    //Static code block
    static {
        System.out.println("Static code block of parent class~");
    }

    //2) Call ordinary code blocks and ordinary member attribute initialization, the priority is the same, the one defined first is called first
    //Member properties
    private String name;
    private int age;

    //Normal code block
    {
        System.out.println("Normal code block of parent class~");
    }

    //3) Call the constructor
    //Constructor
    public Person(String name, int age) {
        System.out.println("Constructor of parent class~");
        this.name = name;
        this.age = age;
    }

    //method
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

5) The call to super and ordinary code blocks is implicit in front of the constructor. The execution order is: super > ordinary code blocks > constructor

public class Test {
    public static void main(String[] args) {
        Stu stu = new Stu();
        //Output: Ordinary code block of the parent class~ Constructor of the parent class~ Ordinary code block of the subclass~ Constructor of the subclass~
    }
}

//father
class Person {
    //code block
    {
        System.out.println("Normal code block of parent class~");
    }

    //Constructor
    public Person() {
        System.out.println("Constructor of parent class~");
    }
}

//Subclass
class Stu extends Person {
    //code block
    {
        System.out.println("Ordinary code block of subclass~");
    }

    //Constructor
    publicStu() {
        System.out.println("Constructor of subclass~");
    }
}

6) When there is an inheritance relationship, after the subclass creates an object, the calling sequence of the class is:

Prerequisite: member = member variable + member method

i. Call the static members of the parent class to initialize the class. If there are multiple static members, priority will be given in the order of definition

ii. Call the static members of the subclass to initialize the class. If there are multiple static members, priority will be given in the order of definition

iii. Call the common members of the parent class to initialize the class. If there are multiple common members, priority will be given in the order of definition

iv. Call the constructor of the parent class

v. Call ordinary members of the subclass to initialize the class. If there are multiple ordinary members, priority will be given in the order of definition

vi. Call the constructor of the subclass

public class Test {
    public static void main(String[] args) {
        Stu stu = new Stu();
        //Output: Static code block of the parent class~ Static code block of the subclass~ Ordinary code block of the parent class~ Constructor of the parent class~ Ordinary code block of the subclass~ Constructor of the subclass~
    }
}

class Person {
    //code block
    static {
        System.out.println("Static code block of parent class~");
    }

    {
        System.out.println("Normal code block of parent class~");
    }

    //Constructor
    public Person() {
        System.out.println("Constructor of parent class~");
    }
}

class Stu extends Person {
    //code block
    static {
        System.out.println("Static code block of subclass~");
    }

    {
        System.out.println("Ordinary code block of subclass~");
    }

    //Constructor
    publicStu() {
        System.out.println("Constructor of subclass~");
    }
}

7) Static code blocks can only call static properties, and ordinary code blocks can be called arbitrarily.

public class Test {
    public static void main(String[] args) {
    }
}

class Person {
    //Static member variables
    private static int id = 52213234;
    
    //Static code block
    static {
        //Static code blocks can only call static members
        //say();//Error
        hi();
        System.out.println(id);
    }

    //Normal code block
    {
        //Ordinary code blocks can call any member
        System.out.println(Person.id);
        say();
        Person.hi();
    }
    
    //Normal method
    public void say() {
        System.out.println("Normal say() method~");
    }
    
    //static method
    public static void hi() {
        System.out.println("static hi() method~");
    }
}

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java skill treeVariables and constants in JavaDefinition of variables 138644 people are learning the system