이것은 바보 같은 질문 일 수도 있지만 클래스의 생성자에서 새 객체를 초기화하거나 객체/객체의 초기화가있는 경우 중요합니다. 대신 클래스 본문에 변수가 있습니까?생성자 또는 클래스 본문에 새 연산자를 배치
public class MyFrame extends JFrame {
private JButton button1;
private JButton button2;
public MyFrame(){
button1 = new JButton("Button1");
button2 = new JButton("Button2");
}
}
public class MyFrame extends JFrame {
private JButton button1 = new JButton("Button1");
private JButton button2 = new JButton("Button2");
public MyFrame(){
}
}
는 일반적으로, 내가 생성자의 클래스 몸과 클래스 필드에서 정적 필드를 정의, 그러나 이것이 내가 선호하는 스타일이다. 그것은 필수 사항이 아닙니다. –