我有一个关于这个场景查询关于静态块
我有2个班 A类和B类内的2个不同的套餐如下的问题。
Package test1 // first Package
public class B
{
public static int q = 10; // having one static variable
static
{
system.out.println ("test")// One static block which is printing something
}
}
第二包因此,在这种情况下
Package test
import test1.B // Importing class B from Package test1
public class A
{
int c = B.q // reading the value q from class B
}
当我将执行类A中的静块是在B类
静态{的System.out.println(”内测试“)}
将被执行或不执行。
请花时间格式化您的代码 - 如果没有缩进,确实很难阅读。 –
接下来,您可以非常轻松地测试您的问题 - 只需运行该代码(在您已解决使用这些包语句编译不了的事实之后...)是否有任何理由不能这样做? –
@ user2256009只要你的B类加载到JVM中,静态块就会被执行。 –