JAVA foundation (keyword --static)

1, static characteristics

  • With loads and loads of class

  • Takes precedence over the object exists

  • All objects are shared classes

        Example: Let the class students should share the same number of classes.

        In fact, this feature is also telling us when to use static?

             If a member variable is shared by all objects, then it should be defined as static.

        For example:

            * Drinking fountains (static modification)

            * Cups (not static modification)

            * Common static, non-static properties

2, static calls

  • It can be invoked by class name

  • In fact, it can call itself by its object name.

  • Recommended to use the class name called.

  • Static modified content in general we call it: related classes, class members

class Demo1_Static {

    public static void main(String[] args) {

        /*Person p1 = new Person();    //创建对象

        p1.name = "苍老师";            //调用姓名属性并赋值

        p1.country = "日本";        //调用国籍属性并赋值

        Person p2 = new Person();

        p2.name = "小泽老师";        //调用姓名属性并赋值

        //p2.country = "日本";        //调用国籍属性并赋值

        p1.speak();

        p2.speak();*/

        Person.country = "日本";    //静态多了一种调用方式,可以通过类名.

        System.out.println(Person.country);

    }

}

class Person {

    String name;                    //姓名

    static String country;                    //国籍

    public void speak() {            //说话的方法

        System.out.println(name + "..." + country);

    }



}
  • Creating objects memory map

 

  • Create static memory map

 

3, static precautions

  • In the static method is not this keyword

    • Static is loaded with class and load, this is with the creation of the object exists.

    • Static object exists than before.

  • Static methods can only access static member variables and static member methods

    • Static methods:

      • Member variables: You can only access static variables

      • Member method: You can only access static member methods

    • Non-static method:

      • Member variable: can be static, it can also be non-static

      • Member method: But is static member method can also be non-static member methods.

  • Jot: Static can only access static.

  • Static like extracting file. Non-static file is compressed.

class Demo2_Static {

    public static void main(String[] args) {

        //Demo d = new Demo();

        //d.print1();





        Demo.print2();

    }

}



class Demo {

    int num1 = 10;                        //非静态的成员变量

    static int num2 = 20;                //静态的成员变量

    public void print1() {                //非静态的成员方法,既可以访问静态的成员也可以访问非静态的

        System.out.println(num1);

        System.out.println(num2);

    }

    public static void print2() {        //静态的成员方法

        //System.out.println(this.num1);//静态的成员方法不能访问非静态的,错误: 无法从静态上下文中引用非静态 变量 num1

        System.out.println(num2);

    }

}

 

4 difference between static variables and member variables

Different [1] belongs

  • Member variables: the object belongs, with the creation of an object exists

  • Static variables: belongs to the class, also known as class variables, and loaded with the loaded class

[2] Different memory location

  • Member variables: the existence of heap memory

  • Static variables: the existence of a static area (shared area) method zone

[3] different memory load time

  • Member variables: With the creation of the object exists, facing the disappearance of the object disappear

  • Static variables: With the load and load class, with the disappearance of class disappear

[4] call different:

  • Member variables: Object variable members

  • Static variables: the class can be called by name. You can also call the object name, it is recommended to use the class name called.


 

class Demo3_Static {

    public static void main(String[] args) {

        //method();                                //错误: 无法从静态上下文中引用非静态 方法 method()

        Demo3_Static.print();                    //在主方法中调用本类的静态方法,可以省略类名.,系统会默认加上

        Demo3_Static d = new Demo3_Static();    //非静态方法在调用的时候必须创建对象调用

        d.method();

    }





    public void method() {                    

        System.out.println("Hello World!");        

    }





    public static void print() {

        System.out.println("Hello World!");

    }

}

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/Cricket_7/article/details/91779557