java static {}的用法 java中static 与private的用法

java\u4e2dstatic\u7684\u7528\u6cd5

1.\u9759\u6001\u65b9\u6cd5
\u901a\u5e38\uff0c\u5728\u4e00\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a\u65b9\u6cd5\u4e3astatic\uff0c\u90a3\u5c31\u662f\u8bf4\uff0c\u65e0\u9700\u672c\u7c7b\u7684\u5bf9\u8c61\u5373\u53ef\u8c03\u7528\u6b64\u65b9\u6cd5
\u58f0\u660e\u4e3astatic\u7684\u65b9\u6cd5\u6709\u4ee5\u4e0b\u51e0\u6761\u9650\u5236\uff1a
\u00b7\u5b83\u4eec\u4ec5\u80fd\u8c03\u7528\u5176\u4ed6\u7684static \u65b9\u6cd5\u3002
\u00b7\u5b83\u4eec\u53ea\u80fd\u8bbf\u95eestatic\u6570\u636e\u3002
\u00b7\u5b83\u4eec\u4e0d\u80fd\u4ee5\u4efb\u4f55\u65b9\u5f0f\u5f15\u7528this \u6216super\u3002
class Simple {
staticvoid go() {
System.out.println("Welcome");
}
}

publicclass Cal {
publicstaticvoid main(String[] args) {
Simple.go();
}
}
\u8c03\u7528\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u5c31\u662f\u201c\u7c7b\u540d.\u65b9\u6cd5\u540d\u201d,\u9759\u6001\u65b9\u6cd5\u7684\u4f7f\u7528\u5f88\u7b80\u5355\u5982\u4e0a\u6240\u793a\u3002\u4e00\u822c\u6765\u8bf4\uff0c\u9759\u6001\u65b9\u6cd5\u5e38\u5e38\u4e3a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5176\u5b83\u7c7b\u63d0\u4f9b\u4e00\u4e9b\u5b9e\u7528\u5de5\u5177\u6240\u7528\uff0c\u5728Java\u7684\u7c7b\u5e93\u4e2d\u5927\u91cf\u7684\u9759\u6001\u65b9\u6cd5\u6b63\u662f\u51fa\u4e8e\u6b64\u76ee\u7684\u800c\u5b9a\u4e49\u7684\u3002

2.\u9759\u6001\u53d8\u91cf
\u58f0\u660e\u4e3astatic\u7684\u53d8\u91cf\u5b9e\u8d28\u4e0a\u5c31\u662f\u5168\u5c40\u53d8\u91cf\u3002\u5f53\u58f0\u660e\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u5e76\u4e0d\u4ea7\u751fstatic\u53d8\u91cf\u7684\u62f7\u8d1d\uff0c\u800c\u662f\u8be5\u7c7b\u6240\u6709\u7684\u5b9e\u4f8b\u53d8\u91cf\u5171\u7528\u540c\u4e00\u4e2astatic\u53d8\u91cf\u3002\u9759\u6001\u53d8\u91cf\u4e0e\u9759\u6001\u65b9\u6cd5\u7c7b\u4f3c\u3002\u6240\u6709\u6b64\u7c7b\u5b9e\u4f8b\u5171\u4eab\u6b64\u9759\u6001\u53d8\u91cf\uff0c\u4e5f\u5c31\u662f\u8bf4\u5728\u7c7b\u88c5\u8f7d\u65f6\uff0c\u53ea\u5206\u914d\u4e00\u5757\u5b58\u50a8\u7a7a\u95f4\uff0c\u6240\u6709\u6b64\u7c7b\u7684\u5bf9\u8c61\u90fd\u53ef\u4ee5\u64cd\u63a7\u6b64\u5757\u5b58\u50a8\u7a7a\u95f4\uff0c\u5f53\u7136\u5bf9\u4e8efinal\u5219\u53e6\u5f53\u522b\u8bba\u4e86
class Value {
staticintc = 0;

staticvoid inc() {
c++;
}
}

publicclass Count2 {
publicstaticvoid prt(String s) {
System.out.print(s);
}

publicstaticvoid main(String[] args) {
Value v1, v2;
v1 = new Value();
v2 = new Value();
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v1.inc();
prt(" v1.c=" + v1.c + " v2.c=" + v2.c);
}
}
\u7ed3\u679c\u4e3a\uff1av1.c=0 v2.c=0 v1.c=1 v2.c=1
\u7531\u6b64\u53ef\u4ee5\u8bc1\u660e\u5b83\u4eec\u5171\u4eab\u4e00\u5757\u5b58\u50a8\u533a\u3002static\u53d8\u91cf\u6709\u70b9\u7c7b\u4f3c\u4e8eC\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\u7684\u6982\u5ff5\u3002

\u503c\u5f97\u63a2\u8ba8\u7684\u662f\u9759\u6001\u53d8\u91cf\u7684\u521d\u59cb\u5316\u95ee\u9898\u3002
\u5982\u679c\u4f60\u9700\u8981\u901a\u8fc7\u8ba1\u7b97\u6765\u521d\u59cb\u5316\u4f60\u7684static\u53d8\u91cf\uff0c\u4f60\u53ef\u4ee5\u58f0\u660e\u4e00\u4e2astatic\u5757\uff0cStatic \u5757\u4ec5\u5728\u8be5\u7c7b\u88ab\u52a0\u8f7d\u65f6\u6267\u884c\u4e00\u6b21\u3002\u4e0b\u9762\u7684\u4f8b\u5b50\u663e\u793a\u7684\u7c7b\u6709\u4e00\u4e2astatic\u65b9\u6cd5\uff0c\u4e00\u4e9bstatic\u53d8\u91cf\uff0c\u4ee5\u53ca\u4e00\u4e2astatic \u521d\u59cb\u5316\u5757\uff1a
class Value3 {
staticintc = 0;

Value3() {
c = 15;
}

Value3(int i) {
c = i;
}

staticvoid inc() {
c++;
}
}

publicclass Count {
publicstaticvoid prt(String s) {
System.out.println(s);
}

Value3 v = new Value3(10);
static Value3 v1, v2;
static {//\u6b64\u5373\u4e3astatic\u5757
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v1 = new Value3(27);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v2 = new Value3(15);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
}

publicstaticvoid main(String[] args) {
Count ct = new Count();
prt("ct.c=" + ct.v.c);
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
v1.inc();
prt("v1.c=" + v1.c + " v2.c=" + v2.c);
prt("ct.c=" + ct.v.c);
}
}

\u7ed3\u679c\u4e3a\uff1a
v1.c=0 v2.c=0
v1.c=27 v2.c=27
v1.c=15 v2.c=15
ct.c=10
v1.c=10 v2.c=10
v1.c=11 v2.c=11
ct.c=11
\u8fd9\u4e2a\u7a0b\u5e8f\u5c55\u793a\u4e86\u9759\u6001\u521d\u59cb\u5316\u7684\u5404\u79cd\u7279\u6027\u3002\u5982\u679c\u4f60\u521d\u6b21\u63a5\u89e6Java\uff0c\u7ed3\u679c\u53ef\u80fd\u4ee4\u4f60\u5403\u60ca\u3002\u53ef\u80fd\u4f1a\u5bf9static\u540e\u52a0\u5927\u62ec\u53f7\u611f\u5230\u56f0\u60d1\u3002\u9996\u5148\u8981\u544a\u8bc9\u4f60\u7684\u662f\uff0cstatic\u5b9a\u4e49\u7684\u53d8\u91cf\u4f1a\u4f18\u5148\u4e8e\u4efb\u4f55\u5176\u5b83\u975estatic\u53d8\u91cf\uff0c\u4e0d\u8bba\u5176\u51fa\u73b0\u7684\u987a\u5e8f\u5982\u4f55\u3002\u6b63\u5982\u5728\u7a0b\u5e8f\u4e2d\u6240\u8868\u73b0\u7684\uff0c\u867d\u7136v\u51fa\u73b0\u5728v1\u548cv2\u7684\u524d\u9762\uff0c\u4f46\u662f\u7ed3\u679c\u5374\u662fv1\u548cv2\u7684\u521d\u59cb\u5316\u5728v\u7684\u524d\u9762\u3002\u5728static{\u540e\u9762\u8ddf\u7740\u4e00\u6bb5\u4ee3\u7801\uff0c\u8fd9\u662f\u7528\u6765\u8fdb\u884c\u663e\u5f0f\u7684\u9759\u6001\u53d8\u91cf\u521d\u59cb\u5316\uff0c\u8fd9\u6bb5\u4ee3\u7801\u53ea\u4f1a\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u4e14\u5728\u7c7b\u88ab\u7b2c\u4e00\u6b21\u88c5\u8f7d\u65f6\u3002\u5982\u679c\u4f60\u80fd\u8bfb\u61c2\u5e76\u7406\u89e3\u8fd9\u6bb5\u4ee3\u7801\uff0c\u4f1a\u5e2e\u52a9\u4f60\u5bf9static\u5173\u952e\u5b57\u7684\u8ba4\u8bc6\u3002\u5728\u6d89\u53ca\u5230\u7ee7\u627f\u7684\u65f6\u5019\uff0c\u4f1a\u5148\u521d\u59cb\u5316\u7236\u7c7b\u7684static\u53d8\u91cf\uff0c\u7136\u540e\u662f\u5b50\u7c7b\u7684\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002

3\uff0e\u9759\u6001\u7c7b
\u901a\u5e38\u4e00\u4e2a\u666e\u901a\u7c7b\u4e0d\u5141\u8bb8\u58f0\u660e\u4e3a\u9759\u6001\u7684\uff0c\u53ea\u6709\u4e00\u4e2a\u5185\u90e8\u7c7b\u624d\u53ef\u4ee5\u3002\u8fd9\u65f6\u8fd9\u4e2a\u58f0\u660e\u4e3a\u9759\u6001\u7684\u5185\u90e8\u7c7b\u53ef\u4ee5\u76f4\u63a5\u4f5c\u4e3a\u4e00\u4e2a\u666e\u901a\u7c7b\u6765\u4f7f\u7528\uff0c\u800c\u4e0d\u9700\u5b9e\u4f8b\u4e00\u4e2a\u5916\u90e8\u7c7b\u3002
publicclass StaticCls {
publicstaticvoid main(String[] args) {
OuterCls.InnerCls oi = newOuterCls.InnerCls();
}
}

classOuterCls {
publicstaticclass InnerCls {
InnerCls() {
System.out.println("InnerCls");
}
}
}
\u7ed3\u679c\u4e3a\uff1aInnerCls

\u3000\u3000private\u662f\u8bbf\u95ee\u6743\u9650\u4fee\u9970\u7b26\uff0c\u7528\u4e8e\u63a7\u5236\u5916\u754c\u5bf9\u7c7b\u5185\u90e8\u6210\u5458\u7684\u8bbf\u95ee\uff0c\u8868\u660e\u5bf9\u8c61\u6210\u5458\u662f\u5b8c\u5168\u79c1\u6709\u7684\uff0c\u4e0d\u5bb9\u8bb8\u5916\u754c\u7684\u4efb\u4f55\u8bbf\u95ee\u3002
\u3000\u3000static\u662f\u9759\u6001\u6210\u5458\u4fee\u9970\u7b26\uff0c\u5176\u4fee\u9970\u7684\u9759\u6001\u53d8\u91cf\u8131\u79bb\u5177\u4f53\u5bf9\u8c61\u72ec\u7acb\u5b58\u5728\uff0c\u5728\u5185\u5b58\u4e2d\u4e4b\u540e\u4e00\u4efd\u62f7\u8d1d\uff0c\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u516c\u7528\u8fd9\u4e00\u4e2a\u5b58\u50a8\u7a7a\u95f4\uff0c\u6240\u4ee5\u5bf9static\u4fee\u9970\u7684\u9759\u6001\u53d8\u91cf\u8fdb\u884c\u7684\u4fee\u6539\u5bf9\u8be5\u7c7b\u7684\u6240\u6709\u5bf9\u8c61\u90fd\u8d77\u4f5c\u7528\u3002static\u4fee\u9970\u7684\u9759\u6001\u51fd\u6570\u4ee3\u8868\u6240\u6709\u5bf9\u8c61\u7684\u7edf\u4e00\u64cd\u4f5c\uff0c\u53ea\u80fd\u8c03\u7528\u9759\u6001\u53d8\u91cf\u3002static\u662f\u9488\u5bf9\u9762\u5411\u5bf9\u8c61\u4e2d\u7684\u201c\u591a\u6001\u201d\u800c\u63d0\u51fa\u6765\u7684\uff0cstatic\u4fee\u9970\u7684\u9759\u6001\u6210\u5458\u4e0d\u5b58\u5728\u591a\u6001\u6027\u3002

static 关键字很有意思哦

public static String getProperty(String key) { 这里不用说了,静态方法

就说说static{}

称为static代码块 ,也叫静态代码块,

是在类中独立于类成员的static语句块,可以有多个,位置可以随便放,它不在任何的方法体内,JVM加载类时会执行这些静态的代码块,如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,每个代码块只会被执行一次

利用静态代码块可以对一些static变量进行赋值

例子

public class Test5 {
private static int a;
private int b;

static{
Test5.a=3;
System.out.println(a);
Test5 t=new Test5();
t.f();
t.b=1000;
System.out.println(t.b);
}
static{
Test5.a=4;
System.out.println(a);
}
public static void main(String[] args) {
// TODO 自动生成方法存根
}
static{
Test5.a=5;
System.out.println(a);
}
public void f(){
System.out.println("hhahhahah");
}
}

运行结果:
3
hhahhahah
1000
4
5

static 属于全局,也就是类的属性 和方法,换句话说 一个类,不管有多少个实例,却只有一个全局变量
class B {static int a=0;}
B b1=new B();..................;B bn=new B();
b1~bn 都是类B的实例,每个实例都共享 变量a,a是全局变量,属于类B的属性,每个实例都能引用变量a,
加入执行b1.a=1;后 那么b2.a,b3.a,b4.a......bn.a 都等于1了,

static 方法也是类似的

需要注意的是 静态属性和方法属于类方法,加载类后,就已经存在静态属性和方法,实例是需要用new构造出来后 才会有引用
根据先后顺序,就有以下两条规则
1、非静态的方法可以调用静态的或者非静态的属性和方法;
2、静态的方法不可以调用非静态的属性和方法,因为非静态的还不一定存在,只能先构造出来后,再通过实例引用
例如 在main方法中 可以直接调用static 的方法,调用非static方法 需要先构造出类的实例,通过实例才能调用方法

这个问题是 实例变量和类变量的区别。用static修饰的变量是类变量,而没有的为实例变量。

不同的对象的实例变量将被分配不同的内存空间,如果类中的成员变量有类变量,那么所以对象的这个类变量都分配给相同的一处内存,改变其中一个对象的这个类变量会影响其他对象的这个类变量,也就是说对象共享类变量。
当java程序执行时,类的字节码文件被加载到内存,如果该类没有创建对象,类实例成员变量不会被分配内存。但是,类中的类变量被加载到内存时,就分配了相应的内存空间。如果该类创建对象,那么不同对象的实例变量互不相同,即分配不同的内存空间,而类变量不再重新分配内存,所有的对象共享类变量,即所有的对象的类变量是相同的一处内存空间,类变量的内存空间直到程序退出运行,才释放所占有的内存。类变量是与类相关联的这个类变量就同时改变了其他对象的这个类变量。因此,类变量不仅可以通过某个对象访问,也可以直接通过类名访问。实例变量仅仅是和相应的对象关联的变量,也就是说,不同对象的实例变量互不相同,即分配不同的内存空间,改变其中一个对象的实例变量不会影响其他对象的这个实例变量。实例变量可以通过对象访问,不能使用类名访问。

首先,你需要知道static{}(静态代码块)在程序加载中static是先于构造方法加载的,并且只会加载一次。
两外static块中只能使用static修饰的属性。
程序中的static{}块只是为了加载properties文件信息,这个加载只会被加载一次。

静态的啦;
就是在程序中只初始化一次而已,只声明一次而已,全局的唯一的。
这段代码的意思就是在程序创建时就加载这个配置文件。

扩展阅读:www.sony.com.cn ... java serversocket ... javascript void ... java remove ... java windowbuilder ... java入门网站 ... javascript 在线 ... java serializable ... java中public static ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网