Learning Java object Construction through an instance

Source: Internet
Author: User

This article provides an example of an error in a project, provides observations and analysis of it, reveals the specific process of an object in the Java language instantiation, and concludes with an important rule for designing Java classes. By reading this article, you can make Java programmers understand the construction of Java objects and design more robust code. This article is suitable for Java beginners and need to improve Java programmer reading.

The program threw an exception

In a project, the author has provided an abstract dialog base class for members of the project team, and the user simply implements an abstract method of the base class in the subclass to draw the interface to display the data, so that the dialog boxes within the project will have the same style. The specific code implementation snippet is as follows (for brevity, other extraneous code is omitted):

public abstract class BaseDlg extends JDialog {
public BaseDlg(Frame frame, String title) {
super(frame, title, true);
this.getContentPane().setLayout(new BorderLayout());
this.getContentPane().add(createHeadPanel(), BorderLayout.NORTH);
this.getContentPane().add(createClientPanel(), BorderLayout.CENTER);
this.getContentPane().add(createButtonPanel(), BorderLayout.SOUTH);
}
private JPanel createHeadPanel() {
... // 创建对话框头部
}
// 创建对话框客户区域,交给子类实现
protected abstract JPanel createClientPanel();
private JPanel createButtonPanel {
... // 创建按钮区域
}
}

This class works well in some code, but when a colleague is in use, the program throws a nullpointerexception violation! After comparison, find out the small differences between the working and abnormal procedures, the code snippets are as follows:

First, the normal work of the code:

public class ChildDlg1 extends BaseDlg {
  JTextField jTextFieldName;
  public ChildDlg1() {
   super(null, "Title");
  }
  public JPanel createClientPanel() {
   jTextFieldName = new JTextField();
   JPanel panel = new JPanel(new FlowLayout());
   panel.add(jTextFieldName);
   ... // 其它代码
   return panel;
  }
  ...
}
ChildDlg1 dlg = new ChildDlg1(frame, "Title"); // 外部的调用

Second, the work is not normal code:

public class ChildDlg2 extends BaseDlg {
  JTextField jTextFieldName = new JTextField();
  public ChildDlg2() {
   super(null, "Title");
  }
  public JPanel createClientPanel() {
   JPanel panel = new JPanel(new FlowLayout());
   panel.add(jTextFieldName);
   ... // 其它代码
   return panel;
  }
  ...
}
ChildDlg2 dlg = new ChildDlg2(); // 外部的调用

Did you see the difference between the two pieces of code? The difference between the two is only the initialization time of the class variable jtextfieldname. After tracing, it is found that jtextfieldname is indeed a null value at the time the Panel.add (jtextfieldname) statement is executed.

As we know, Java allows you to assign an initial value to a variable while defining a class variable. When you need to create an object during the operation of the system, you first allocate the memory space for the object, and then initialize the variable in the order of the definition of the variable in the class before calling any method before calling the constructor method of the class. So, in this case, why does the code that is initialized when the variable is defined have a null pointer violation?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.