zoukankan      html  css  js  c++  java
  • static关键字

    static关键字的引入:

    举例1:

     1 class Person {
     2     String name;
     3     int age;
     4     String country;
     5 
     6     public Person(){
     7     }
     8     public Person(String name, int age){
     9         this.name = name;
    10         this.age = age;
    11     }
    12     public Person(String name, int age, String country){
    13         this.name = name;
    14         this.age = age;
    15         this.country = country;
    16     }
    17 
    18     public void show() {
    19         System.out.println("name:" + name + " age:" + age + " country:" + country);
    20     }
    21 }
    22 
    23 public class StaticTest {
    24     public static void main(String[] args) {
    25         Person p1 = new Person("张三",23,"中国");
    26         p1.show();
    27         Person p2 = new Person("李四",54,"中国");
    28         p2.show();
    29         Person p3 = new Person("王五",44,"中国");
    30         p3.show();
    31         Person p4 = new Person("黎明",90,"中国");
    32         p4.show();
    33 
    34     }
    35 }

    输出:

    name:张三 age:23 country:中国
    name:李四 age:54 country:中国
    name:王五 age:44 country:中国
    name:黎明 age:90 country:中国

    由输出结果可知:

    姓名和年龄都是变化的,但是他们拥有相同的国籍,我每次在创建对象的时候,堆内存都会为国籍这个成员变量分配内存空间,这样就有点浪费内存了,所以针对多个

    对象拥有相同的成员变量值的时候,Java语言就提供了一个关键字来修饰:static

    案例2:

    public static void main(String[] args) {
            Person p1 = new Person("张三",23,"中国");
            p1.show();
            Person p2 = new Person("李四",54);
            p2.show();
            Person p3 = new Person("王五",44);
            p3.show();
    
        }

    输出:

    name:张三 age:23 country:中国
    name:李四 age:54 country:null
    name:王五 age:44 country:null

    案例3:

    class Person {
        String name;
        int age;
        static String country;  //注意这里相比案例1与案例2多了static修饰符
    
        public Person(){
        }
        public Person(String name, int age){
            this.name = name;
            this.age = age;
        }
        public Person(String name, int age, String country){
            this.name = name;
            this.age = age;
            this.country = country;
        }
    
        public void show() {
            System.out.println("name:" + name + " age:" + age + " country:" + country);
        }
    }
    
    public class StaticTest {
        public static void main(String[] args) {
            Person p1 = new Person("张三",23,"中国");
            p1.show();
            Person p2 = new Person("李四",54);
            p2.show();
            Person p3 = new Person("王五",44);
            p3.show();
    
        }
    }

     输出:

    name:张三 age:23 country:中国
    name:李四 age:54 country:中国
    name:王五 age:44 country:中国

    案例4:

     1 class Person {
     2     String name;
     3     int age;
     4     static String country;
     5 
     6     public Person(){
     7     }
     8     public Person(String name, int age){
     9         this.name = name;
    10         this.age = age;
    11     }
    12     public Person(String name, int age, String country){
    13         this.name = name;
    14         this.age = age;
    15         this.country = country;
    16     }
    17 
    18     public void show() {
    19         System.out.println("name:" + name + " age:" + age + " country:" + country);
    20     }
    21 }
    22 
    23 public class StaticTest {
    24     public static void main(String[] args) {
    25         Person p1 = new Person("张三",23,"中国");
    26         p1.show();
    27 
    28         Person p2 = new Person("李四",54);
    29         p2.show();
    30         Person p3 = new Person("王五",44);
    31         p3.show();
    32 
    33         p3.country = "美国";
    34         p3.show();
    35         p1.show();
    36         p2.show();
    37     }
    38 }

    输出:

    name:张三 age:23 country:中国
    name:李四 age:54 country:中国
    name:王五 age:44 country:中国
    name:王五 age:44 country:美国
    name:张三 age:23 country:美国
    name:李四 age:54 country:美国

    static关键字的特点:

      1. static关键字可以修饰成员变量和成员方法

      2. 随着类的加载而加载

      3. 优先于对象存在

      4. 被类的所有对象共享

        这也是我们判断是否使用静态关键字的条件

      5. 可以通过类名直接调用

        当然也可以通过对象名调用,但是我们推荐使用类名调用

        静态修饰的内容一般我们称其为:与类相关的,类成员

        非静态的内容我们一般称其为:对象成员

      6. 静态的成员变量被所有该类的对象共享

    static关键字注意事项:

      1. 在静态方法中是没有this关键字的

        如何理解呢?

        静态是随着类的加载而加载,this是随着对象的创建而存在

        静态比对象先存在

      2. 静态方法只能访问静态的成员变量和静态的成员方法。

        如何理解呢?

        因为静态是随着类的加载而加载,而方法是随着对象的调用而存在,静态方法要优先与非静态方法存在

        非静态方法既可以访问静态的成员变量静态的成员方法也可以访问非静态的成员变量和非静态的成员方法

    静态变量和成员变量的区别:

        所属不同

          静态变量属于类,所以也被称为类变量

          成员变量属于对象,所以也被称为实例变量或对象变量

        内存中位置不同

          静态变量存储于方法区的静态区

          成员变量存储于堆内存

        内存出现时间不同

          静态变量随着类的加载而加载,随着类的消失而消失

          成员变量随着对象的创建而存在,随着对象的消失而消失

        调用不同

          静态变量既可以通过类名直接调用,也可以通过对象名调用

          成员变量只能通过对象名调用

     

  • 相关阅读:
    NumPy笔记:运算符(exp,sqrt,square)
    NumPy笔记:常用操作
    php字符操作
    laravel如何实现批量插入
    php中@符号的作用
    laravel如何实现数据的批量插入
    如何在laravel框架中使用阿里云的oss
    laravle如何设置mysql数据表前缀
    thinkphp视图中如何调用并且向控制器传参数
    thinkphp如何隐藏入口文件index.php
  • 原文地址:https://www.cnblogs.com/happy520/p/6672511.html
Copyright © 2011-2022 走看看