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

    在类中,用static声明的成员变量为静态成员变量,也称为类变量。 类变量的生命周期和类相同,在整个应用程序执行期间都有效。它有如下特点:

      1. 为该类的公用变量,属于类,被该类的所有实例共享,在类被载入时被显式初始化。

      2. 对于该类的所有对象来说,static成员变量只有一份。被该类的所有对象共享!!

      3. 一般用“类名.类属性/方法”来调用。(也可以通过对象引用或类名(不需要实例化)访问静态成员。)

      4. 在static方法中不可直接访问非static的成员。

    核心要点:

                static修饰的成员变量和方法,从属于类。

                普通变量和方法从属于对象的。

    【示例4-10】static关键字的使用

    /**
     * 测试static关键字的用法
     * @author 高淇
     *
     */
    public class User2 {
        int id; // id
        String name; // 账户名
        String pwd; // 密码
         
        static String company = "北京尚学堂"; // 公司名称
         
         
        public User2(int id, String name) {
            this.id = id;
            this.name = name;
        }
         
        public void login() {
            printCompany();
            System.out.println(company); 
            System.out.println("登录:" + name);
        }
         
        public static void printCompany() {
    //         login();//调用非静态成员,编译就会报错
            System.out.println(company);
        }
         
        public static void main(String[] args) {
            User2 u = new User2(101, "高小七");
            User2.printCompany();
            User2.company = "北京阿里爷爷";
            User2.printCompany();
        }
    }

      运行结果如图4-9所示。

    1.png

    2.png

    图4-10 示例4-10的内存分配图

    /**
     * 测试static关键字的用法
     * @author 
     *
     */
    public class User2 {
        int id; // id
        String name; // 账户名
        String pwd; // 密码
         
        static String company = "北京尚学堂"; // 公司名称
         
         
        public User2(int id, String name) {
            this.id = id;
            this.name = name;
        }
         
        public void login() {
            printCompany();
            System.out.println(company); 
            System.out.println("登录:" + name);
        }
         
        public static void printCompany() {
    //         login();//调用非静态成员,编译就会报错
            System.out.println(company);
        }
         
        public static void main(String[] args) {
            User2 u = new User2(101, "高小七");
            User2.printCompany();
            User2.company = "北京阿里爷爷";
            User2.printCompany();
        }
    }
  • 相关阅读:
    多路复用与设置阻塞、非阻塞模式
    ['\xef\xbb\xbf这个什么含义? PY技术开发交流区 乐讯手机高手
    fcntl使用 and_tt 博客园
    Linux 设备驱动 Edition 3Linux设备驱动第三版(中文版)
    CRT source Google 搜索
    BOM–字节序标记 永不放弃的地盘 博客频道 CSDN.NET
    在C语言中,unsigned char是什么类型?_百度知道
    The JR Concurrent Programming Language
    C语言:为什么用fprintf(stderr,"Error");比printf("Error");更好?
    bash
  • 原文地址:https://www.cnblogs.com/long-holiday/p/10102402.html
Copyright © 2011-2022 走看看