Java static

本文深入探讨了Java中静态变量、静态方法及静态初始化块的概念与使用。通过具体示例展示了静态成员如何随类加载而初始化,并说明了它们在整个程序运行期间的全局特性。此外,还解释了静态方法的调用方式及其作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

include static block, static method, static variable



public class Test {

 
    
public static void main(String[] args)  {
    
// 调用静态方法
    staticTest();
    }

    
    
/**
     * 静态方法的调用
     
*/

    
private static void staticTest() {
    
    Apple a 
= new Apple();
    System.
out.println(a.count);
    
    a.count 
= 20;
    
    System.
out.println("second phrase");
    Apple b 
= new Apple();
    System.
out.println(a.count);
    System.
out.println(b.count);
    
    System.
out.println("third phrase");
    Apple c 
= new Apple();
    System.
out.println(a.count);
    System.
out.println(b.count);
    System.
out.println(c.count);
    
    System.
out.println("four phrase");
    Apple d 
= new Apple(100);
    System.
out.println(a.count);
    System.
out.println(b.count);
    System.
out.println(c.count);
    System.
out.println(d.count);
    
    
// 直接调用类的静态方法
    d.setCount(1000);
    System.
out.println(a.count);
    System.
out.println(b.count);
    System.
out.println(c.count);
    System.
out.println(d.count);
    }


}


class Apple{
    
    
public static int count = 2;
    
    
/**
     * 静态初始化块,随着类进行初始化工作
     
*/

    
static {
    System.
out.println(count);
    count 
= 10;
    System.
out.println("static block test");
    System.
out.println(count);
    }

    
    
/**
     * 构造函数
     
*/

    
public Apple() {
    count
++;
    System.
out.println("Apple number: "+count);
    }

    
    
/**
     * 类的static块
     
*/

    
static {
    System.
out.println("static end");
    }

    
    
/**
     * 带参数的构造函数
     * @param initCount
     
*/

    
public Apple(int initCount){
    
this.count = initCount;
    }

    
    
/**
     * 调用静态初始化
     * @param i
     
*/

    
public static void setCount(int i) {
    count 
= i;
    }

}


 

run results:

2
static block test
10
static end
Apple number: 
11
11
second phrase
Apple number: 
21
21
21
third phrase
Apple number: 
22
22
22
22
four phrase
100
100
100
100
1000
1000
1000
1000

 Another example:

 

import java.util.Arrays;



public class Test {

 
    
public static void main(String[] args)  {


    Temp.i 
= 192;
    System.
out.println(Temp.i);
    
    Temp t 
= new Temp();
    System.
out.println(t.i);
    }

        

}


class Temp {
    
    
static {
    i 
= 10;
    }

    
    
public static int i ;
    
    
static{
    System.
out.println(i);
    i
++;
    System.
out.println(i);
    }

}



run results:

10
11
192
192

 

To summary: Class init the static var when class init only once, then the var become global, and the static should be accessed only in static but not absolutly. and static method can be called by using Class name, and static method invoke static method....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值