static 关键字的用法

 1.static关键字修饰的(静态方法)

使用static关键字修饰的方法的调用方式

调用方式1:如果是在当前类的static方法中,则直接调用

调用方式2:如果是在其他类的static方法中,则需要通过类名.方法()调用

备注1:普通方法(没有使用staitc关键字修饰的方法)允许调用static修饰的方法

备注2: static修饰的方法(静态方法)不允许调用普通方法

package com.ztt.Demo01;//static关键字修饰的(静态方法)
public class test2 {public static void main(String[] args) {doSth();test2.doSth();}//使用static关键字修饰的方法的调用方式//调用方式1:如果是在当前类的static方法中,则直接调用//调用方式2:如果是在其他类的static方法中,则需要通过类名.方法()调用//备注1:普通方法(没有使用staitc关键字修饰的方法)允许调用static修饰的方法//备注2: static修饰的方法(静态方法)不允许调用普通方法public static void doSth() {//不允许调用普通方法//normal();}public  void normal() {}}

2.静态方法和普通方法之间的调用关系总结:

静态方法和普通方法之间的调用关系总结:

//静态方法“允许”调用静态方法

//普通方法“允许”调用静态方法\其它普通方法

 

package com.ztt.Demo01;//静态方法和普通方法之间的调用关系总结:
//静态方法“允许”调用静态方法
//普通方法“允许”调用静态方法\其它普通方法public class test3 {public static void main(String[] args) {//调用静态方法Father.doSth();//通过类名调用//调用普通方法(实例方法)Father f=new Father();//创建对象(实例)f.work();//调用普通方法//f.doSth();//调用静态方法(不推荐)}}class Father{//定义的方法形式//静态方法:使用static关键字修饰的方法public static void doSth() { //在静态方法中,不允许调用普通方法//this.work();}//普通方法:实例方法,没有static 关键字修饰public void work() { //普通方法中,允许调用static静态方法doSth();}
}

3.static关键字修饰的静态代码块 

静态代码块:类被“加载”时,静态代码块自动执行

构造代码块:每次调用构造方法前,构造代码块自动执行

static关键字修饰的静态代码块

//执行顺序

静态代码块(先父类、再子类)=>父类构造代码块=>父类构造方法=>子类构造代码块=>子类构造方法

package com.ztt.Demo01;
//static关键字修饰的静态代码块
//执行顺序
//静态代码块(先父类、再子类)=>父类构造代码块=>父类构造方法=>子类构造代码块=>子类构造方法
public class test4 {//main主函数所在的类必须被加载static {System.out.println("test4的静态代码块!");}public static void main(String[] args) {
//		Person p1=new Person();
//		Person p2=new Person();
//		
//		Object obj=new Object();User u1=new User();User u2=new User();}}
//父类
class Person{//静态代码块:类被“加载”时,静态代码块自动执行static {System.out.println("Person父类静态代码块1执行ing......");}static {System.out.println("Person父类静态代码块2执行ing......");}//构造代码块:每次调用构造方法前,构造代码块自动执行{System.out.println("Person父类构造代码块执行ing......");}public Person() {System.out.println("Person类的无参构造方法执行ing...");}
}//子类
class User extends Person{static {System.out.println("User子类的静态代码块执行ing......");}{System.out.println("User子类的构造代码块执行ing......");}public User() {System.out.println("User子类的无参构造方法执行ing...");}
}

运行结果:

test4的静态代码块!
Person父类静态代码块1执行ing......
Person父类静态代码块2执行ing......
User子类的静态代码块执行ing......
Person父类构造代码块执行ing......
Person类的无参构造方法执行ing...
User子类的构造代码块执行ing......
User子类的无参构造方法执行ing...
Person父类构造代码块执行ing......
Person类的无参构造方法执行ing...
User子类的构造代码块执行ing......
User子类的无参构造方法执行ing...

4.static 关键字修饰的静态变量

普通成员变量:通过实例对象

静态成员变量:可以通过实例对象或类名 

package com.ztt.Demo01;
//static 关键字修饰的静态变量
public class test5 {public static void main(String[] args) {//普通成员变量:通过实例对象
//		Counter c1=new Counter();
//		c1.value++;
//		
//		Counter c2=new Counter();
//		c2.value++;
//		c2.value++;
//		c2.value++;
//		
//		System.out.println("c1的value="+c1.value);
//		System.out.println("c2的value="+c2.value);//静态成员变量:可以通过实例对象或类名// c1.count++;Counter c1 = new Counter();c1.count++;Counter c2 = new Counter();c2.count++;c2.count++;c2.count++;System.out.println("c1的count="+c1.count);System.out.println( "c2的count="+c2.count) ;}}class Counter{//成员变量int value;//普通成员变量(通过实例)static int count;//静态成员变量(通过实例或类名)}

运行结果:

c1的count=4
c2的count=4
package com.ztt.Demo01;public class test6 {public static void main(String[] args) {// 创建3个优惠券对象Coupon coupon1=new Coupon();Coupon coupon2=new Coupon();Coupon coupon3=new Coupon();Coupon coupon4=new Coupon();Coupon coupon5=new Coupon();System.out.println("创建Coupon优惠券对象的个数为:"+Coupon.count);//预期输出结果为:5System.out.println("优惠券1的编号为:"+coupon1);//预期输出为:1001System.out.println("优惠券2的编号为:"+coupon2);//预期输出为:1002System.out.println("优惠券3的编号为:"+coupon3);//预期输出为:1003System.out.println("优惠券4的编号为:"+coupon4);//预期输出为:1004System.out.println("优惠券5的编号为:"+coupon5);//预期输出为:1005}}
//优惠券
class Coupon{private int id;//优惠券编号private static int idVal=1000;//编号记录器public static int count=0;//对象计数器public Coupon(){this.id=++idVal;count++;}@Overridepublic String toString() {return String.valueOf(id);}
}

运行结果:

创建Coupon优惠券对象的个数为:5
优惠券1的编号为:1001
优惠券2的编号为:1002
优惠券3的编号为:1003
优惠券4的编号为:1004
优惠券5的编号为:1005
package com.ztt.Demo01;import java.time.LocalDate;public class test7 {public static void main(String[] args) {// TODO Auto-generated method stubRecord[] recordArray = {new Record( LocalDate.of( 2020,1,15)),new Record( LocalDate.of( 2020,3,17)),new Record(LocalDate.of( 2021,6,19)),new Record( LocalDate.of(2020,2,21)),new Record(LocalDate.of( 2021,1,25))};for(Record record : recordArray) {//判断每个record记录对象是否超时,都会重复创建beginDate和endDateSystem.out.println(record.isTimeout());}}}class Record{private LocalDate writeDate;//记录写入日期private static LocalDate beginDate;//开始private static LocalDate endDate;//结束static {beginDate = LocalDate.ofYearDay(2020, 1);//开始日期2020年1月1日endDate = LocalDate.ofYearDay(2021, 1);//结束日期2021年1月1日}//构造方法:创建记录时,传入该记录的写入日期public Record(LocalDate date) {this.writeDate = date;}//判断记录写入日期是否超时public boolean isTimeout() {//检查"记录写入日期"是否 >= 开始时间 并且 < 结束日期return writeDate.compareTo(beginDate) >= 0 && writeDate.compareTo(endDate) < 0;}
}

运行结果:

true
true
false
true
false

看代码!!!!!

package com.ztt.Demo02;public class Test1 {static int value = 33;public static void main(String[] args) throws Exception{new Test1().printValue();}private void printValue(){int value = 3;System.out.println(this.value);}
}

运行结果:

33
package com.ztt.Demo02;public class Test2 {public static void main(String[] args) {new Test();}
}class Test extends Base{static{System.out.println("test static");}public Test(){System.out.println("test constructor");}}class Base{ static{System.out.println("base static");}public Base(){System.out.println("base constructor");}
}

运行结果:

base static
test static
base constructor
test constructor
package com.ztt.Demo02;public class Test3 {static{System.out.println("test static 1");}public static void main(String[] args) {}static{System.out.println("test static 2");}
}

运行结果:

test static 1
test static 2
package com.ztt.Demo02;public class Test4 {int a = 1;static int b = 1;// 无参Test4() {a++;b++;}// 有参Test4(int c) {this.a = this.a + c;this.b = this.b + c;}public static void main(String[] args) {Test4 t1 = new Test4(); // a=2 b=2Test4 t2 = new Test4(2);System.out.println(t1.a); // 2System.out.println(t1.b); // 4System.out.println(t2.a); // 3System.out.println(t2.b); // 4}
}
2
4
3
4
package com.ztt.Demo02;public class Test5 {static {int x = 5;}static int x, y;public static void main(String args[]) {x--;myMethod();//x+y;//1+ ++x;System.out.println(x + y + ++x);}public static void myMethod() {//++x;//y=x+x;//x++;y = x++ + ++x;}
}
3
package com.ztt.Demo02;public class Test6 {static int a = 1;static int b = 2;public static void main(String[] args) {// static String c = "3"; // 编译出错//static 不能修饰局部变量final String c = "3";System.out.println(a + b + c); // 33System.out.println(c + a + b); // 312}
}

运行结果:

33
312
package com.ztt.Demo02;public class Test7 {// 先创建static修饰的静态成员变量(对象)public static Test7 t1 = new Test7();public static Test7 t2 = new Test7();{System.out.println("构造块");}static{System.out.println("静态块");}public static void main(String[] args){Test7 t3 = new Test7();}
}

运行结果:

构造块
构造块
静态块
构造块

// 1.父类的静态代码块

// 2.子类的静态代码块

// 3.父类的构造代码块(父类中的对象person1创建)

// 3.1 父类的构造方法

// 4.子类的构造代码块(子类中的对象person2创建)

// 4.1子类的构造方法

// 1. 加载Test8类

// 1.1 执行Test8类的静态代码块(父类)

// 2.加载MyClass类

// 2.1 执行MyClass类的静态代码块(子类)

// 3.创建Test8类中的Person对象(执行父类的构造行为)

// 3.1 加载Person类,执行Person类的静态代码块

// 3.2 执行Person类的构造方法

// 4.执行Test8类的构造方法

// 5.创建MyClass类中的Person对象

// 5.1 执行Person类的构造方法

// 6. 执行MyClass的构造方法

 

package com.ztt.Demo02;public class Test8 {// 先加载Person类,再执行Person类的构造方法Person person1 = new Person("Test");//相当于构造代码块static {System.out.println("test static");}public Test8() {System.out.println("test constructor");}public static void main(String[] args) {// 1.父类的静态代码块// 2.子类的静态代码块// 3.父类的构造代码块(父类中的对象person1创建)// 3.1 父类的构造方法// 4.子类的构造代码块(子类中的对象person2创建)// 4.1子类的构造方法// 1. 加载Test8类// 1.1 执行Test8类的静态代码块(父类)// 2.加载MyClass类// 2.1 执行MyClass类的静态代码块(子类)// 3.创建Test8类中的Person对象(执行父类的构造行为)// 3.1 加载Person类,执行Person类的静态代码块// 3.2 执行Person类的构造方法// 4.执行Test8类的构造方法// 5.创建MyClass类中的Person对象// 5.1 执行Person类的构造方法// 6. 执行MyClass的构造方法new MyClass();}
}class Person {static {System.out.println("person static");}public Person(String str) {System.out.println("person " + str);}
}class MyClass extends Test8 {// 执行Person类的构造方法Person person2 = new Person("MyClass");//相当于构造代码块static {System.out.println("myclass static");}public MyClass() {System.out.println("myclass constructor");}
}

运行结果:

test static
myclass static
person static
person Test
test constructor
person MyClass
myclass constructor
package com.ztt.Demo02;public class Test9 {public static void main( String[] args) {Example e = new Example();}
}
class Example{Demo demo1 =new Demo() ;Demo demo2 =new Demo();static {System.out.println( "Example类的static" ) ;}{System.out.println( "Example类的构造代码块");}public Example() {System.out.println( "Example类的构造方法");}
}
class Demo{static {System. out.println( "Demo类的static" );}{System.out.println( "Demo类的构造代码块");}public Demo(){System.out.println( "Demo类的构造方法");}
}

运行结果:

Example类的static
Demo类的static
Demo类的构造代码块
Demo类的构造方法
Demo类的构造代码块
Demo类的构造方法
Example类的构造代码块
Example类的构造方法

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/636613.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

裸辞后的焦虑与挑战:如何成功跨行业找到满意工作?

裸辞后的焦虑 在最近一个平常的晚上&#xff0c;我吃完饭后像往常一样坐在沙发上休息。突然&#xff0c;电话铃声响起&#xff0c;是来自久未联系的姐夫。寒暄过后&#xff0c;他透露出他的焦虑&#xff1a;外甥女小桦自从四五个月前辞职后&#xff0c;至今还没有找到新的工作…

【成本价特惠】招募证书代理:工信部、PMP、阿里云、华为等认证,机会难得!

扫码和我联系 亲爱的读者朋友们&#xff0c; 今天&#xff0c;我想和大家分享一个难得的机会。我们目前正在积极招募各类证书的代理&#xff0c;包括工信部的证书、PMP&#xff08;项目管理专业人士&#xff09;证书、阿里云证书、华为证书、OCP 证书、CFA 证书等。这些证书在…

最大流—EK算法,流网络,残留网络,定理证明,详细代码

文章目录 零、卡车运输一、流网络1.1流网络1.2流1.3最大流1.4残留网络1.5增广路径1.6流网络的割1.7最大流最小割定理1.7.1证明 1.8Ford-Fulkerson方法 二、Edmonds-Karp算法2.1定义2.2EK算法的实现2.3EK算法详细代码2.4OJ练习 零、卡车运输 Lucky Puck公司有冰球工厂Vancouver…

Unity导出Android项目踩坑记录

导出的时候需要注意以下地方的配置&#xff1a; 1、buildSetting-> 设置ExportProject 2、buildsetting ->playerSetting ->设置IL2CPP 3、设置ndk edit->preferences->external tools->ndk 如果unity的ndk版本和android项目里的ndk版本不一致会报错&…

【Qt开发】初识Qt

文章目录 1. Qt的背景1.1 Qt是什么1.2 Qt的发展史1.3 Qt支持的平台 2. Qt开发环境的搭建2.1 Qt SDK下载2.2 Qt SDK的安装 3. 一个简单的Qt模板程序的创建4. Qt模板程序的代码讲解4.1 main.cpp4.2 widget.h4.3 widget.cpp4.4 widget.ui4.5 test_1_18.pro4.6 一些中间文件 5. Qt在…

keil软件仿真

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、pandas是什么&#xff1f;二、使用步骤 1.引入库2.读入数据总结 前言 例如&#xff1a;随着人工智能的不断发展&#xff0c;机器学习这门技术也越来越重要…

ubuntu opencv 编译contrib库

OpenCV4.6.0 注意opencv的版本和opencv_contrib的版本需要一致&#xff0c;反正我编译opencv-4.6.0和opencv_contrib_4.x不成功。 提前安装GTK库&#xff0c;不然不能用imshow这些显示的功能。 sudo apt install libgtk2.0-dev # git clone https://github.com/opencv/opencv…

vue基于Spring Boot框架的甘肃敦煌文化旅游管理系统

本敦煌文化旅游管理系统是为了提高用户查阅信息的效率和管理人员管理信息的工作效率&#xff0c;可以快速存储大量数据&#xff0c;还有信息检索功能&#xff0c;这大大的满足了用户和管理员这两者的需求。操作简单易懂&#xff0c;合理分析各个模块的功能&#xff0c;尽可能优…

MySQL的数据类型

整数类型&#xff08;Integer Types&#xff09;&#xff1a; TINYINT: 1字节&#xff0c;范围从-128到127或0到255&#xff08;无符号&#xff09;。 SMALLINT: 2字节&#xff0c;范围从-32,768到32,767或0到65,535&#xff08;无符号&#xff09;。 MEDIUMINT: 3字节&#xf…

(蓝桥杯每日一题)平方末尾及补充(常用的字符串函数功能)

能够表示为某个整数的平方的数字称为“平方数 虽然无法立即说出某个数是平方数&#xff0c;但经常可以断定某个数不是平方数。因为平方数的末位只可能是:0,1,4,5,6,9 这 6 个数字中的某个。所以&#xff0c;4325435332 必然不是平方数。 如果给你一个 2 位或 2 位以上的数字&am…

六、标准对话框、多应用窗体

一、标准对话框 Qt提供了一些常用的标准对话框&#xff0c;如打开文件对话框、选择颜色对话框、信息提示和确认选择对话框、标准输入对话框等。1、预定义标准对话框 &#xff08;1&#xff09;QFileDialog 文件对话框 QString getOpenFileName() 打开一个文件QstringList ge…

You need to add dependency of ‘poi-ooxml‘ to your project, and version >= 4.1.2

原因 由于在依赖中引用了多个版本的 hutool,导致在最终打包时使用的版本不是由在开发时所引用的版本 cn.hutool.core.exceptions.DependencyException: You need to add dependency of poi-ooxml to your project, and version > 4.1.2at cn.hutool.poi.excel.ExcelUtil.get…

MyBatis-Plus 日常操作

本文主要介绍 mybatis-plus 日常操作。 一、快速开始 本文基于 springboot、maven、jdk1.8、mysql 环境。 新建如下数据库&#xff1a; 建议大家选择 utf8mb4 这种字符集&#xff0c;做过微信的同学应该会知道&#xff0c;微信用户名称的表情&#xff0c;是需要这种字符集才…

基于python旅游推荐系统 协同过滤算法 爬虫 Echarts可视化 Django框架(源码)✅

毕业设计&#xff1a;2023-2024年计算机专业毕业设计选题汇总&#xff08;建议收藏&#xff09; 毕业设计&#xff1a;2023-2024年最新最全计算机专业毕设选题推荐汇总 &#x1f345;感兴趣的可以先收藏起来&#xff0c;点赞、关注不迷路&#xff0c;大家在毕设选题&#xff…

Android14之DefaultKeyedVector实现(一百八十二)

简介&#xff1a; CSDN博客专家&#xff0c;专注Android/Linux系统&#xff0c;分享多mic语音方案、音视频、编解码等技术&#xff0c;与大家一起成长&#xff01; 优质专栏&#xff1a;Audio工程师进阶系列【原创干货持续更新中……】&#x1f680; 优质专栏&#xff1a;多媒…

计算机组成原理 第一弹

ps&#xff1a;本文章的图片来源都是来自于湖科大教书匠高老师的视频&#xff0c;声明&#xff1a;仅供自己复习&#xff0c;里面加上了自己的理解 这里附上视频链接地址&#xff1a;1-2 计算机的发展_哔哩哔哩_bilibili ​​ 目录 &#x1f680;计算机系统 &#x1f680;计…

基于SpringBoot的欢乐校园管理系统

文章目录 项目介绍主要功能截图&#xff1a;部分代码展示设计总结项目获取方式 &#x1f345; 作者主页&#xff1a;超级无敌暴龙战士塔塔开 &#x1f345; 简介&#xff1a;Java领域优质创作者&#x1f3c6;、 简历模板、学习资料、面试题库【关注我&#xff0c;都给你】 &…

SpringMVC搭建环境

idea创建java项目后添加webapp怎么配置 1.首先在main下创建一个普通文件webapp 2. 3.选中你的项目&#xff0c;添加Web 4.修改这两处的路径&#xff0c;修改为你webapp所在的路径 先修改左下角的路径&#xff0c;然后再添加web.xml. 然后再修改右上角的地址&#xff0c;注…

Python 零散

列表推导式 列表推导式是一种在一行代码中创建列表的紧凑语法。它允许你使用一种简洁的方式从一个可迭代对象&#xff08;例如列表、元组、字符串等&#xff09;中生成新的列表。列表推导式的一般形式如下&#xff1a; new_list [expression for item in iterable if …

基于SpringBoot Vue家政服务预约平台系统

大家好✌&#xff01;我是Dwzun。很高兴你能来阅读我&#xff0c;我会陆续更新Java后端、前端、数据库、项目案例等相关知识点总结&#xff0c;还为大家分享优质的实战项目&#xff0c;本人在Java项目开发领域有多年的经验&#xff0c;陆续会更新更多优质的Java实战项目&#x…