栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

static

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

static

static:静态的 唯一

- 不由static修饰的属性,类可以创建对象,每个对象都有一份自己的属性

- 由static修饰的属性与方法,属于类,且只有一份,直接由类名调用

- 静态方法中不能直接使用方法外的非静态变量

- 静态常量:命名要求全字母大写,命名尽量清晰

- static final int num=3;

public class StaticClass  {
    static int num;           //不管对象的事儿
           int age;           //每个对象都有一份
    final  int id=1;
}
public class Entrance {
    public static void main(String[] args) {
        int count=10;                    //局部变量
        System.out.println(count);
        StaticClass sc0=new StaticClass();
        StaticClass sc1=new StaticClass();
        StaticClass sc2=new StaticClass();

        sc0.num=100;
        sc0.age=20;
        sc1.num=1000;
        sc1.age=25;
        sc2.num=10000;
        sc2.age=30;

        System.out.println (sc0.num);
        System.out.println (sc0.age);

        System.out.println (sc1.num);
        System.out.println (sc1.age);

        System.out.println (sc2.num);
        System.out.println (sc2.age);

        System.out.println (StaticClass.num);
    }

    //静态方法中,不能直接调用属性变量/常量,必须确定属性时哪个对象调用
    //    System.out.println(age);
    //    System.out.println(id);
    //静态方法中,可以直接调用静态变量/常量
    //    System.out.println(num);


}

结果如下:

D:Javabinjava.exe "-javaagent:D:JetrainsIntelliJ IDEA 2021.2.1libidea_rt.jar=64250:D:JetrainsIntelliJ IDEA 2021.2.1bin" -Dfile.encoding=UTF-8 -classpath C:UsersBabyLuDesktop学习javauntitledoutproductionuntitled Entrance
10
10000
20
10000
25
10000
30
10000

Process finished with exit code 0

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/709393.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号