[英]Understanding reflection's strange behavior
I was writing this piece of code to understand reflection and encountered one scenario where I couldn't really figure out the reason for the codes' behavior.我正在编写这段代码来理解反射,并遇到了一个我无法真正弄清楚代码行为的原因的场景。 Hopefully I receive some guidance from the community.
希望我能得到社区的一些指导。
Following is my test model class & here, for every instantiation, I want to know the exact number of instances created during runtime (using reflection)以下是我的测试 model class & 在这里,对于每个实例化,我想知道在运行时创建的实例的确切数量(使用反射)
public final class Model {
private static final Model instance = new Model("Testing");
private static int count = 0;
private String name;
private Model(String name) {
this.name = name;
++count;
}
public static Model getInstance() {
return instance;
}
public static int getInstanceCount() {
return count;
}
public String getName() {
return name;
}
public void doSomething() {
try {
System.out.println("Shh.... I am trying to do something");
Thread.sleep(1000);
System.out.println("Ok! Done.");
return;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
System.out.println("Oops! I failed in doing your job...");
}
}
The driver code for this scenario is as follows,该场景的驱动代码如下,
public class ReflectionTest {
public static void main(String[] args) throws Exception {
Model.getInstance().doSomething();
System.out.println(Model.getInstanceCount());
Constructor<?>[] constructor = Model.class.getDeclaredConstructors();
for (Constructor<?> aConstructor : constructor) {
aConstructor.setAccessible(true);
Model m = (Model) aConstructor.newInstance("Testing through Reflection");
System.out.println(m.getName());
m.doSomething();
System.out.println(m.getInstanceCount());
//System.out.println(Model.getInstanceCount());
}
}
}
The output for this above piece of code came out to be as follows,上面这段代码的 output 出来如下,
Shh.... I am trying to do something
Ok! Done.
0
Testing through Reflection
Shh.... I am trying to do something
Ok! Done.
1
As you can see, the instance count came out to be 1. I expected it to be as 2.如您所见,实例数为 1。我预计它为 2。
However, I changed the test model class's constructor as shown below.但是,我更改了测试 model 类的构造函数,如下所示。 The datatype of count is now changed to Integer, instead of previously set 'int' .
count 的数据类型现在更改为 Integer,而不是之前设置的 'int' 。
private Model(String name) {
this.name = name;
if (count == null)
count = 0;
++count;
}
Surprisingly, I get the correct value for the instance count.令人惊讶的是,我得到了正确的实例计数值。
Shh.... I am trying to do something
Ok! Done.
1
Testing through Reflection
Shh.... I am trying to do something
Ok! Done.
2
This might be a silly question, but I am not able to ponder on what really happened behind the scenes.这可能是一个愚蠢的问题,但我无法思考幕后究竟发生了什么。 I need some guidance from the community on this.
我需要社区对此的一些指导。
Thanks in advance.提前致谢。
This has nothing to do with reflection.这与反思无关。
private static final Model instance = new Model("Testing");
private static int count = 0;
The initializers are executed in order.初始化程序按顺序执行。 So:
所以:
private static final Model instance = new Model("Testing");
Executing the constructor causes count
to be incremented from 0 to 1, but then:执行构造函数会导致
count
从 0 增加到 1,但随后:
private static int count = 0;
Sets count back to zero.将计数设置回零。
Reverse the order of the declarations.颠倒声明的顺序。
private static int count = 0;
private static final Model instance = new Model("Testing");
Or omit the initializer on count
(its default value is zero anyway).或者省略
count
的初始化器(无论如何它的默认值为零)。
private static final Model instance = new Model("Testing");
private static int count;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.