当前位置:网站首页>静态方法static学习
静态方法static学习
2022-06-10 22:52:00 【號先生】
首先:
1:用static修饰一个变量是静态变量
2:静态变量,直接使用类名,静态变量名赋值
3:静态变量在内存中只有一份
4:静态的方法就是不需要将类实例化就可以直接使用类名调用到方法或者属性。
一:如果两个方法都是普通方法,那么可以直接用方法名调用
二:如果两个方法都是静态方法, 那么可以直接用方法名调用
三:如果在静态方法中调用非静态方法,那么需要先创建对象,再通过对象名.方法名()
因此,需先创建对象,其次是对象名.方法名()
四:在非静态方法中,可以使用静态方法和静态变量
package Static;
import lombok.Data;
/**
* Created with IDEA
* author:HCG
**/
@Data
public class StaticExample {
//静态变量
private static int count; //保持私有,仅能靠setter方法访问
public static String str;
//静态块
static{
//在类被加载的时候用于初始化某些资源
System.out.println("StaticExample static block");
//仅能访问静态变量和静态方法
str="Test";
setCount(2);
}
//可以在一个类中有多个静态块
static{
System.out.println("StaticExample static block2");
}
public int getCount() {
return count;
}
//静态方法
public static void setCount(int count) {
if(count > 0)
StaticExample.count = count;
}
//静态工具方法
public static int addInts(int i, int...js){
int sum=i;
for(int x : js) sum+=x;
return sum;
}
//静态类的例子,方便打包之用
public static class MyStaticClass{
public int count;
}
public static void main(String[] args) {
int i = addInts(8);
}
}
边栏推荐
- [opencv practice] in cold winter, there will be a rainbow. Do you love this special effect?
- How to measure the refresh rate of oscilloscope
- [appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)
- Typecho blog site wide deployment of Tencent cloud CDN tutorial - Xingze V Club
- MultipartFile重命名上传
- 示波器刷新率怎么测量
- 【颜值检测神器】来,请拿出你们的绝活(这颜值,对得起观众么?)
- [MVC&Core]ASP.NET Core MVC 视图传值入门
- [pyGame] when the "coolest snake in history" arrives, fun will play it (no money for fun)
- yum源更新
猜你喜欢
![[pyGame games] here it is. This Gobang game is super A. share it with your friends~](/img/76/faea3558ed6fadff755c517922088b.png)
[pyGame games] here it is. This Gobang game is super A. share it with your friends~

IGBT与三代半导体SiC双脉冲测试方案
![[pyGame games] I'm not afraid you can't walk the maze series: the ultimate AI walks the maze. After learning, it will take you to open the door to a new world ~ (with game source code)](/img/57/dcf291b044b5e5860a7fdc817076d5.jpg)
[pyGame games] I'm not afraid you can't walk the maze series: the ultimate AI walks the maze. After learning, it will take you to open the door to a new world ~ (with game source code)

【AI出牌器】第一次见这么“刺激”的斗地主,胜率高的关键因素竟是......

【自动回复小脚本】新年快乐,每一个字都是我亲自手打的,不是转发哦~

Hyperleger fabric installation

【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)

Dark horse headlines - Tencent's salary system reform has caused controversy; Intel expanded the recruitment of female engineers nationwide; Black horse is 100% employed really

A simple understanding of B tree

【Pygame小游戏】激荡大脑思维,一起来玩转奇思妙想“24点”叭~(超赞滴)
随机推荐
【Pygame小游戏】来了来了它来了——这款五子棋小游戏超A的,分享给你的小伙伴儿一起pk吧~
csdn每日一练——有序表的折半查找
2022 college entrance examination quantitative paper | please answer the questions for quantitative candidates
Basic introduction and core components of kubernetes
【Pygame合集】回忆杀-“童年游戏”,看看你中几枪?(附五款源码自取)
启牛学堂理财可靠吗,安全吗
【AcWing】4. Multiple knapsack problem I
LabVIEW obtains the information of all points found by the clamp function
希尔排序
【漫天烟花】绚烂烟花点亮夜空也太美了叭、某程序员携带烟花秀给大家拜年啦~
C language file operation
[latex] latex vs Code Snippets
[opencv practice] in cold winter, there will be a rainbow. Do you love this special effect?
About optimizing API interface response speed
[pyGame] when the "coolest snake in history" arrives, fun will play it (no money for fun)
[turtle confessions collection] "the moon at the bottom of the sea is the moon in the sky, and the person in front of us is the sweetheart." Be happy for the rest of your life, and be safe for ever ~
WinDriver compilation summary
Shell Sort
curl导入postman报错小记
LabVIEW error "memory full - Application stopped on node"