不使用该字段的值

private int count=0;之前,我在eclipse上得到了上述警告private int count=0;

这是我的代码:

 package synchronise; public class Mysync { private int count=0; public Mysync() { // TODO Auto-generated constructor stub } public static void main(String[] args) { // TODO Auto-generated method stub Mysync sync = new Mysync(); sync.dowork(); } public void dowork(){ Thread mysync = new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub for(int i=0; i<2000; i++){ // System.out.println() count++; } } }); Thread mysync2 = new Thread(new Runnable() { public void run() { // TODO Auto-generated method stub for(int i=0; i<2000; i++){ // System.out.println() count++; } } }); mysync.start(); mysync2.start(); try { mysync.join(); mysync.join(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

我怎样才能解决这个问题? 为什么我收到此错误? 我更新了代码。

如果要修复警告,只需使用它即可。

例如。

 System.out.println(count); 

警告将消失。

警告表明编译代码时,优化可能会删除无用的代码。

这里没用的代码意味着它与输出无关,因为软件都是关于输入和输出的。

您对count唯一用法是更新它 – 因为您实际上从未对此值执行任何操作 ,因此可以安全地删除它,以及增加它的位置。