2014-04-11 124 views
2

我新的Java和我有一个问题,当我尝试做一个公共/私有变量 例: private int varName; 的Eclipse给我一个错误: Illegal modifier for parameter count; only final is permitted参数计数非法修饰符;只有最终被允许

+2

你能不能显示代码?否则你会最终得到一些随机猜测。 –

回答

8

局部变量和参数不能有publicprivate修饰符。你只能给他们final。甚至不能使用static

4

您不能将访问级别的修饰符在方法参数上。他们只能在班级成员中接受。此外,这是没有任何意义的,因为参数不能在方法范围之外访问。

+0

@ user3421750想一想。那些修饰语在那里有什么好处?反正你不能在方法外访问局部变量。 –

+2

甚至没有语法来尝试*从该方法之外的任何地方引用方法参数。也许你应该解释你在做什么后,而不是问你的眼前的问题。 –

0

这通常会发生,每当我们试图访问变量,它是局部的,我们试图访问它在匿名类方法象下面这样:所以在这里

JButton button=new JButton(); 
     int a=5; 
     button.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent arg0) { 
       // TODO Auto-generated method stub 
       System.out.println(""+a);//Compiler Error:Cannot refer to a non-final variable a inside an inner class defined in a different method 
      } 
     }); 

变量“a”必须是最后的或类变量在匿名类方法中进行访问。