Chapter 10 object oriented programming (advanced part)

Class variable

  • What are class variables

    Class variables, also known as static variables / static attributes, are variables shared by all objects of the class. Any object area of the class accesses it and gets the same value. Similarly, when any object of this class modifies it, the same variable is modified

  • Define syntax:

    (1) Access modifier static, data type, variable name (commonly used)

    (2) static access modifier data type variable name;

Note: class variables are created with class loading, so they can be accessed even if no object instance is created

  • Usage details:

    1. When do I need to use class variables

      When we need to make all objects of a class share a variable, we can consider the experimental class variable (static variable).

2. Difference between class variable and instance variable:

	Class variables are shared by all objects of the class, while instance variables are exclusive to each object.

3. Adding static is called class variable or static variable, otherwise it is called instance variable

4. Class variables can be accessed through class name, class variable name or object name and class variable name

5. Instance variable cannot pass class name. Class variable name

6. Class variables are initialized when the class is loaded, that is, even if you do not create an object, as long as the class is loaded, the variable can be used

7. The life cycle of class variables starts with the loading of the class and ends with the extinction of the class

  • example:

public class Test {
    public static void main(String[] args) {
        child child1 = new child("jack");
        child.payfee(100);
        child1.payfee(200);
        child1.showfee();
        //child.showfee() will report an error... / / reason: the show method is not a static method here and cannot be called directly
        System.out.println(child.sum(20,30));//You can simplify the code for calling methods
    }
}
class child{
    private String name;
    public  static double fee= 0;

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

    public static void payfee(double fee){
        child.fee += fee;
    }
    public void showfee(){
        System.out.println(fee);
    }
    public static double sum(double n1,double n2){
        return n1 + n2;
    }
}

 

  • matters needing attention:

    (1) Class methods and ordinary methods are loaded with the loading of classes, and the structure information is stored in the method area

    !! No this parameter in class method

    this parameter is implicit in ordinary methods

    (2) Ordinary methods are related to objects and need to be called through object names, such as object names. Method names (parameters) cannot be called through class names

    (3) Object related keywords are not allowed in class methods, such as this and super. Common methods are allowed

    (4) Static methods (class methods) can only access static variables or static methods

Summary: yours (static method / static variable) is mine (ordinary method / ordinary variable), mine or mine

  • Exercise question: see if there are any errors in the following code. If there are any errors, modify them and ask the final value of total

public class Test {
    public static void main(String[] args){
        Person.setTotal(2);
        Person.out();
        new Person();
        Person.out();
    }
}
class Person {
    private int id;
    private static int total;
    public static void setTotal(int total){
        //this.total; / / error, this keyword cannot be used in static methods
        Person.total = total;
    }
    public static void out(){
        System.out.println("total="+total);
    }
    public Person(){
        total++;
        id = total;//Non static methods can access static parameters
    }
}

main method

  • In depth understanding of main method:

    Explain the form of the main method: public static void main(String[] args) {}

    (1) The main: Main () method is called by the java virtual machine

    (2) Static: the java virtual machine needs to call the main () method of the class, so the access permission of this method must be static

    (3) The java virtual machine does not have to create an object when executing the main () method, so the method must be static

    (4) String [] * *: * * this method receives array parameters of string type, which saves the parameters passed to the running class when executing java commands

    (5) args: when java executes the program, the parameters are passed in, such as java Hello sample Tom Jack. At this time, three strings are passed into the program

  • !! Note:

(1) In the main () method, we can directly call the static method or static property of the class where the main method is located

(2) However, you cannot directly access the non static members in this class. You must create an instance object of this class before you can access the non static members in this class through this object

Tags: Java Back-end

Posted on Thu, 02 Dec 2021 18:41:27 -0500 by jacksonpt