在構造函數中聲明+實例化變量,而不是在構造函數中聲明外部並僅實例化,或者甚至在外部聲明+實例化兩者之間有什麼優勢?在java GUI構造函數中聲明和實例化變量的好處
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);
}
}
那麼,其中一個,在構造函數中聲明的變量立即超出範圍被垃圾收集...... –