当前位置: 首页 > news >正文

Java第二阶段---11封装---第四节 static 修饰符

1.static 修饰符应用范围

static修饰符只能用来修饰类中定义的成员变量、成员方法、代码块以及内部类(内部类有专门章节进行讲解)。

2.static 修饰成员变量

static 修饰的成员变量称之为类变量。属于该类所有成员共享。

示例

package cn.lyxq.test04;public class ChinesePeople {private String name;private  int age;public static String country = "中国";public ChinesePeople(String name,int age){this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}
}
package cn.lyxq.test04;public class ChinesePeopleTest {public static void main(String[] args) {ChinesePeople cp1 = new ChinesePeople("张三",20);System.out.println(ChinesePeople.country);ChinesePeople.country="日本";ChinesePeople cp2 = new ChinesePeople("李四",30);System.out.println(ChinesePeople.country);ChinesePeople cp3 = new ChinesePeople("王五",32);System.out.println(ChinesePeople.country);}}

如果类变量是公开的,那么可以使用 类名.变量名 直接访问该类变量

3.static 修饰成员方法

static 修饰的成员方法称之为类方法,属于该类所有成员共享。

package cn.lyxq.test04;public class ChinesePeople {private String name;private  int age;private static String country = "中国";public ChinesePeople(String name,int age){this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//类方法public static String getCountry() {return country;}//类方法public static void setCountry(String country) {ChinesePeople.country = country;}
}
package cn.lyxq.test04;public class ChinesePeopleTest {public static void main(String[] args) {ChinesePeople cp1 = new ChinesePeople("张三",20);ChinesePeople.setCountry("日本");ChinesePeople cp2 = new ChinesePeople("李四",30);System.out.println(ChinesePeople.getCountry());ChinesePeople cp3 = new ChinesePeople("王五",32);System.out.println(cp3.getCountry());//不是最优访问方式}}

如果类方法是公开的,那么可以使用 类名.方法名直接访问该类方法。

4.static 修饰代码块

static 修饰的代码块被称为静态代码块,在JVM第一次记载该类时执行。因此,静态代码代码块只能执行一次,通常用于一些系统设置场景。

package cn.lyxq.test04;public class ChinesePeople {private String name;private  int age;//使用static修饰的成员变量称为类变量,不会随着成员变化而变化,属于所有成员共享private static String country ;//static修饰的代码块称为静态代码块,在JVM第一次加载该类的时候执行,只能执行一次static{country = "中国";System.out.println("country属性已经被赋值");}public ChinesePeople(String name,int age){this.name = name;this.age = age;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}//类方法public static String getCountry() {return country;}//类方法public static void setCountry(String country) {ChinesePeople.country = country;}
}
package cn.lyxq.test04.test;import  cn.lyxq.test04.ChinesePeople;public class ChinesePeopleTest {public static void main(String[] args) {ChinesePeople cp = new ChinesePeople("张三",20);}}

5.static内存

http://www.lryc.cn/news/454919.html

相关文章:

  • 【C/C++】错题记录(五)
  • 关系数据库标准语言SQL(11,12)
  • Oracle 11g RAC 节点异常重启问题分析
  • vscode 中显示 pnpm : 无法加载文件 C:\Users\AppData\Roaming\npm\pnpm.ps1,因为在此系统上禁止运行脚本
  • C嘎嘎入门篇:类和对象番外(时间类)
  • Spring Boot项目实战教程:快速构建Web应用与RESTful API
  • OpenAI 开发者大会!实时语音功能有API了,GPT-4o支持多模态微调,上下文cache功能上线
  • 解决ros2 rviz Fixed Frame No TF data问题
  • Python数据分析篇--NumPy--进阶
  • 基于Arduino的宠物食物分配器
  • make和Makefile
  • 【数学分析笔记】第4章第4节 复合函数求导法则及其应用(2)
  • 【预备理论知识——2】深度学习:线性代数概述
  • 【目标检测】yolo的三种数据集格式
  • 数据分析案例-机器学习工程师薪资数据可视化分析
  • Django连接Dify、ChatGPT4o并计算tokens数量方法
  • 面试系列-淘天提前批面试
  • 计算机中科学中有哪些空间换时间的操作??
  • Mac安装Manim并运行
  • leetcode58:最后一个单词的长度
  • 18448 最小生成树
  • 前端工程化 - Vue
  • 使用 NVIDIA H100 上的 Azure 机密计算释放隐私保护 AI 的潜力
  • 目标检测与图像分类:有什么区别?各自的使用场景是什么?
  • Lua 数据类型
  • 复现文章:R语言复现文章画图
  • 东方仙盟——软件终端架构思维———未来之窗行业应用跨平台架构
  • 支持向量机(SVM)基础教程
  • Python小示例——质地不均匀的硬币概率统计
  • 京东web 京东e卡绑定 第二部分分析