当前位置:网站首页>静态方法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);
}
}
边栏推荐
- 【 pygame Games 】 don't find, Leisure Games Theme come 丨 Bubble Dragon applet - - Leisure Games Development recommendation
- Deepin20菜单启动选项后自检到iwlwifi停机
- Binary tree pruning
- Compared with the "South-to-North Water Transfer", what will the "east to west" of the fire bring to cloud computing?
- 都说验证码是爬虫中的一道坎,看我只用五行代码就突破它。
- Easyrecovery15 simple and convenient data recovery tool
- CSDN daily practice -- half search of ordered table
- 【Pygame小游戏】剧情流推荐:什么样的游戏才能获得大家的喜欢呢?(魔鬼恋人、霸总娇妻版)
- After deepin20 menu startup option, the self-test indicates that iwlwwifi is stopped
- From the perspective of Confucius Temple IP crossover, we can see how the six walnuts become "butterflies" for the second time
猜你喜欢

Bluetooth development (2) -- initialization

Exception 0xc00000005 code occurred when LabVIEW called DLL

【Pygame小游戏】“史上最炫酷贪吃蛇”驾到,FUN开玩(不好玩不要钱)
![[turtle confessions collection]](/img/97/6f1008386931bcb93655d94121e2c4.png)
[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 ~

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

Leetcode-560 and subarray with K
![[AI card player] for the first time to see such an](/img/7b/f1965a74733e152dcc542e465112b0.png)
[AI card player] for the first time to see such an "exciting" landowner, the key factor for a high winning rate is

【Opencv实战】这个印章“神器”够牛,节省了时间提高了效率,厉害~(附完整源码)
![[pyGame collection] memory killing -](/img/97/10a4333662b49ac35e5b7433a5e6a4.png)
[pyGame collection] memory killing - "Childhood Games", how many shots did you get? (attach five source codes for self access)

Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
随机推荐
Serial port missing in Ni Max in LabVIEW
基于华为云ECS的目标检测与识别的昇腾AI开发体验【华为云至简致远】
【Pygame小游戏】“史上最炫酷贪吃蛇”驾到,FUN开玩(不好玩不要钱)
Redis installation and common problem solving based on centeros7 (explanation with pictures)
Deepin20菜单启动选项后自检到iwlwifi停机
[AI card player] for the first time to see such an "exciting" landowner, the key factor for a high winning rate is
IGBT and third generation semiconductor SiC double pulse test scheme
Self made app connected to onenet --- realize data monitoring and distribution control (mqtt)
[latex] latex vs Code Snippets
[pyGame games] story stream recommendation: what kind of games can you like? (devil lover, bully's wife version)
[pyGame games] interesting puzzle game: how many hamsters can you play? (source code attached)
[pyGame games] don't look for it. Here comes the leisure game topic - bubble dragon widget - recommendation for leisure game research and development
【Opencv实战】寒冷的冬季,也会迎来漫天彩虹,这特效你爱了嘛?
【Pygame小游戏】别找了,休闲游戏专题来了丨泡泡龙小程序——休闲游戏研发推荐
【Turtle表白合集】“海底月是天上月,眼前人是心上人。”余生多喜乐,长平安~(附3款源码)
希尔排序
CSDN daily practice - find the closest element and output the subscript
Bluetooth development (4) -- about flow control
csdn每日一练——找出最接近元素并输出下标
Method of converting file to multipartfile