Java static block静态快

本文探讨了静态变量和静态块在类实例化之前的初始化过程。静态变量独立于类实例存在,即使对象引用为null,静态变量仍会被初始化并持有其初始值。如果将变量声明为非静态,则在尝试访问前必须先创建类的实例。

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

static variables or static blocks in a class will be initialised before the class gets instantiated. Also, we all know that the static variables are not tied up with the instances. So even if we have the reference as null, the variable s value had already been initalised to "hello". So calling it that way will print the values Inside Method and Hello as the variable call will be made with the class name as they are tied up with the class . If you make the variable non - static , you will get a NPE.

 

静态变量和静态块在类被实例化之前,已经得到初始化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值