【发布时间】:2014-02-14 14:25:44
【问题描述】:
在构造函数中声明+实例化变量,而不是在外面声明然后在构造函数中实例化,甚至在外面都声明+实例化有什么好处?
public class GUIview extends JFrame {
public GUIview() {
JPanel pan = new JPanel();
JSplitPane splitPane = new JSplitPane();
}
public static void main(String[] args) {
GUIview view = new GUIview();
view.setVisible(true);
}
}
或
public class GUIview extends JFrame {
JPanel pan;
JSplitPane splitPane;
public GUIview() {
pan = new JPanel();
splitPane = new JSplitPane();
}
public static void main(String[] args) {
GUIview view = new GUIview();
view.setVisible(true);
}
}
【问题讨论】:
-
好吧,首先,像这样在构造函数中声明的变量会立即超出范围进行垃圾回收......
标签: java user-interface constructor declaration instantiation