局部内部类引用外部对象时为什么必须指定为final? 不要说是"规则"什么的,我想知道原理,谢谢! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我可以作一个反面的假设来理解.假设 它所引用的外部对象可以不是 final 的也就是说 内部类 可以对这个对象进行任意修改.如果这个传入的对象还在其他地方被引用这种修改对它们来说是透明的.所以回出现一些意想不到的结果.所以出于这个原因. sun 要把它设计为 final 的把. 我在学习这一块的时候也不太理解为什么内部类在调用外围类的变量里,外围类的变量必须为final期待达人解惑 别死记!~import java.awt.*;import javax.swing.*;import java.awt.event.*;public class SwingApplication { private static int numClicks=0; //private static JLabel l; public static void main(String args[]) { JFrame f = new JFrame("Java10-[C.R.S.M]K01"); JButton b = new JButton("Swing Button"); final JLabel l = new JLabel("You have click: " + "0 " + "次"); JPanel p = new JPanel(); b.addActionListener(new ButtonHandler()); p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30)); p.setLayout(new GridLayout(0, 1)); f.add(p); p.add(b); p.add(l); f.setSize(200, 200); f.setVisible(true); } static class ButtonHandler implements ActionListener { // 加上 public 也不对。 public void actionPerformed(ActionEvent e) { numClicks++; l.setText("You have click: " + numClicks + "次");// 输出l后面那个点时没有弹出对应的方法。 } }}这个程序是有问题的!~改成这样就对了:import java.awt.*;import javax.swing.*;import java.awt.event.*;public class SwingApplication { private static int numClicks=0; private static JLabel l; public static void main(String args[]) { JFrame f = new JFrame("Java10-[C.R.S.M]K01"); JButton b = new JButton("Swing Button"); l = new JLabel("You have click: " + "0 " + "次"); JPanel p = new JPanel(); b.addActionListener(new ButtonHandler()); p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30)); p.setLayout(new GridLayout(0, 1)); f.add(p); p.add(b); p.add(l); f.setSize(200, 200); f.setVisible(true); } static class ButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { numClicks++; l.setText("You have click: " + numClicks + "次"); } }}呵呵,这是我今天的问题。帖子地址:http://topic.csdn.net/u/20080413/11/febf2907-031c-427d-98d1-412978c4cc97.html?seed=926383211 创建了几个对象 字符流读文件乱码,救急 class.getResource()?? Log4j问题,请高人指教!! 如何理解addNotify方法和文本域中的列长度? 很的JAVA资料下载网站,快进!!! Image,ImageIcon,Icon的不同处和各在什么情况下用 真心求助 如何实现求差数列? 取小数位数的问题 jtable单元格问题 怎样在JAVA中添加音乐
假设 它所引用的外部对象可以不是 final 的
也就是说 内部类 可以对这个对象进行任意修改.
如果这个传入的对象还在其他地方被引用
这种修改对它们来说是透明的.所以回出现一些意想不到的结果.
所以出于这个原因. sun 要把它设计为 final 的把.
import java.awt.*;import javax.swing.*;
import java.awt.event.*;public class SwingApplication {
private static int numClicks=0;
//private static JLabel l;
public static void main(String args[]) {
JFrame f = new JFrame("Java10-[C.R.S.M]K01");
JButton b = new JButton("Swing Button");
final JLabel l = new JLabel("You have click: " + "0 " + "次");
JPanel p = new JPanel();
b.addActionListener(new ButtonHandler());
p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
p.setLayout(new GridLayout(0, 1));
f.add(p);
p.add(b);
p.add(l);
f.setSize(200, 200);
f.setVisible(true);
} static class ButtonHandler implements ActionListener { // 加上 public 也不对。
public void actionPerformed(ActionEvent e) { numClicks++;
l.setText("You have click: " + numClicks + "次");// 输出l后面那个点时没有弹出对应的方法。
}
}
}这个程序是有问题的!~
改成这样就对了:
import java.awt.*;import javax.swing.*;
import java.awt.event.*;public class SwingApplication {
private static int numClicks=0;
private static JLabel l;
public static void main(String args[]) {
JFrame f = new JFrame("Java10-[C.R.S.M]K01");
JButton b = new JButton("Swing Button");
l = new JLabel("You have click: " + "0 " + "次");
JPanel p = new JPanel();
b.addActionListener(new ButtonHandler());
p.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 30));
p.setLayout(new GridLayout(0, 1));
f.add(p);
p.add(b);
p.add(l);
f.setSize(200, 200);
f.setVisible(true);
} static class ButtonHandler implements ActionListener {
public void actionPerformed(ActionEvent e) { numClicks++;
l.setText("You have click: " + numClicks + "次");
}
}
}呵呵,这是我今天的问题。
帖子地址:http://topic.csdn.net/u/20080413/11/febf2907-031c-427d-98d1-412978c4cc97.html?seed=926383211