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. 静态方法只能访问静态的成员变量和静态的成员方法。

        如何理解呢?

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

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

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

        所属不同

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

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

        内存中位置不同

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

          成员变量存储于堆内存

        内存出现时间不同

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

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

        调用不同

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

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

     

  • 相关阅读:
    Atitit alldiaryindex v1 t717 目录 1. Fix 1 2. Diary detail 1 2.1. Diary 1987---2016.12 1 2.2. Diary20
    Atitit web httphandler的实现 java python node.js c# net php 目录 1.1. Java 过滤器 servelet 1 1.2. Python的
    Atitit 网关协议cgi wsgi fcgi fastcgi 目录 1.1. CGI(common gateway unterface) 1 1.2. 2.1 WSGI: 1 1.3. 2.3
    Atitit gui的实现模式文本 dom ast 像素绘图api native 目录 1. Pl ast xml domAst 1 1.1. 简介 1 1.1.1. 【具体语法树】 2 2.
    Atitit 遍历文件夹目录解决方案与规范 attilax总结 1. 规范 2 1.1. 注意的不要同时改变文件夹内容,增删文件。这样获取到的目录list不会变化 2 1.2. 主义中文名称文件读写
    Atitit node.js自定义模块化 Function walkFileTree() exports.walkFileTree =walkFileTree 引用  Files=require
    Atitit node.js问题解决总结t99 目录 1.1. 找不到node程序 1 1.2. GBK编码问题 1 2. 按行读取gbk编码问题 2 2.1. 先写入txt utf8 encode
    Atitit io读取文件法 目录 1. 文件法 1 1.1. 异步读取文件: 1 1.2. 2.同步读取方法 1 1.3. 二进制读文件: 1 2. 读取api规范 1 3. Atitit 按照
    Atitit 按照行读取文件 目录 1.1. 类库'readline' 1 1.2. 类库C:workspacedataindexGenerNodejssdkioFileUtils.js
    Atitit sumdoc index 2019 v6 t56 .docx Atitit sumdoc index s99 目录 1. Zip ver 1 1.1. C:UsersAdminis
  • 原文地址:https://www.cnblogs.com/happy520/p/6672511.html
Copyright © 2011-2022 走看看