当前位置:网站首页>静态方法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);
}
}
边栏推荐
- [appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)
- About optimizing API interface response speed
- 2022年高考量化卷|请各位量化考生答题
- MySQL命令行导入导出数据
- 452. 用最少数量的箭引爆气球
- 集合删除元素技巧 removeIf
- Four ways to add names to threads in the thread pool
- [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 ~
- [opencv practice] this seal "artifact" is awesome, saving time and improving efficiency. It is powerful ~ (complete source code attached)
- 30 | how to reset the consumer group displacement?
猜你喜欢
![[pyGame games] interesting puzzle game: how many hamsters can you play? (source code attached)](/img/88/733cddca32491c4ac45102aa703815.jpg)
[pyGame games] interesting puzzle game: how many hamsters can you play? (source code attached)

Leetcode-209 minimum length subarray
![[pyGame] stir up your brain and play the](/img/0c/fd558c843705af19720d790da4ff06.jpg)
[pyGame] stir up your brain and play the "24 o'clock" idea together ~ (awesome)

【AcWing】4. Multiple knapsack problem I

【漫天烟花】绚烂烟花点亮夜空也太美了叭、某程序员携带烟花秀给大家拜年啦~
![[MVC&Core]ASP.NET Core MVC 视图传值入门](/img/c2/3e69cda2fed396505b5aa5888b9e5f.png)
[MVC&Core]ASP.NET Core MVC 视图传值入门

Exception 0xc00000005 code occurred when LabVIEW called DLL
![[auto reply Script] happy new year. I typed every word myself, not forwarded it~](/img/53/75d2bbbee653a41e206ebd751fdea1.png)
[auto reply Script] happy new year. I typed every word myself, not forwarded it~

【自动回复or提醒小助手】妈妈再也不用担心我漏掉消息了(10行代码系列)

vtk. VTP download in JS
随机推荐
MP框架基本操作(自用)
Binary tree pruning
From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
Error report of curl import postman
[pyGame] this "groundhog" game is going to be popular (come on, come on)
763. 划分字母区间
Pseudo static setting of Typecho - starze V Club
Insert sort
【无标题】
【Pygame小游戏】不怕你走不过系列:极致AI走迷宫,学习完带你打开新世界大门~(附游戏源码)
The website is harmed by XSS hanging horse
示波器和频谱分析仪的区别
都说验证码是爬虫中的一道坎,看我只用五行代码就突破它。
【Pygame小游戏】“史上最炫酷贪吃蛇”驾到,FUN开玩(不好玩不要钱)
Select sort
[appearance detection artifact] come on, please show me your unique skill (is this appearance worthy of the audience?)
Bluetooth (5) -- about retransmission
LabVIEW pictures look bright or dark after being cast from 16 bits to 8 bits
2022年高考量化卷|请各位量化考生答题
Wireshake introduction learning notes