如何在java中删除现有对象并创建新对象?
我想删除在另一个类中创建的类的上一个对象,并在它之后创建一个新对象 以下是我的部分代码:如何在java中删除现有对象并创建新对象?,java,object,Java,Object,我想删除在另一个类中创建的类的上一个对象,并在它之后创建一个新对象 以下是我的部分代码: //DataBase DataBase data_base; Document financial_document; /** * Creates new form FinancialDocumentsJFrame */ public FinancialDocumentsJFrame() { frameWidth = this.getWidth(); frameHeight = t
//DataBase
DataBase data_base;
Document financial_document;
/**
* Creates new form FinancialDocumentsJFrame
*/
public FinancialDocumentsJFrame() {
frameWidth = this.getWidth();
frameHeight = this.getHeight();
panelWidth = frameWidth;
panelHeight = frameHeight;
data_base = new DataBase();
code_table_hashmap = data_base.code_table;
activity_table_hashmap = data_base.activity_table;
client_table_hashmap = data_base.client_table;
employee_table_hashmap = data_base.employee_table;
initComponents();
// Sets size of the frame.
if(false) // Full screen mode
{
// Disables decorations for this frame.
//this.setUndecorated(true);
// Puts the frame to full screen.
this.setExtendedState(this.MAXIMIZED_BOTH);
}
else // Window mode
{
// Size of the frame.
this.setSize(1100, 800);
// Puts frame to center of the screen.
this.setLocationRelativeTo(null);
// So that frame cannot be resizable by the user.
this.setResizable(false);
}
}
我在类之前声明了对象,并在类的一个方法中创建了一个新对象
现在我需要在另一个方法中删除这个对象并创建一个新的
这是我的密码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (bill_documentType==1){
fd_document_type = true;
}
else if (bill_documentType==0){
fd_document_type = false;
}
else {
System.out.println("Error in Document Type!!!!!!!!!");
}
if (bill_documentStatus==1){
fd_document_status = true;
}
else if (bill_documentStatus==0){
fd_document_status = false;
}
else {
System.out.println("Error in Document Status!!!!!!!!!");
}
if (jTextField1.getText().equals("") || jTextField2.getText().equals("") || jTextField3.getText().equals("")|| jTextField4.getText().equals("")|| jTextField5.getText().equals("")|| bill_documentStatus==-1){
JOptionPane.showConfirmDialog(null, "لطفا همه فيلد ها را پر كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (jTextField1.getText().length()!=7 || jTextField2.getText().length()!=10 || jTextField3.getText().length()>10 || jTextField5.getText().length()>10) {
JOptionPane.showConfirmDialog(null, "ورودي نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentNumberText(jTextField1.getText())){
JOptionPane.showConfirmDialog(null, "ورودي شماره سند نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentDateText(jTextField2.getText())){
JOptionPane.showConfirmDialog(null, "ورودي تارخ سند نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentClientNumberText(jTextField3.getText())){
JOptionPane.showConfirmDialog(null, "ورودي شماره مشتري نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentAmountText(jTextField4.getText())){
JOptionPane.showConfirmDialog(null, "ورودي مبلغ نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentEmployeeNumberText(jTextField5.getText())) {
JOptionPane.showConfirmDialog(null, "ورودي شماره پرسنلي نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentEmployeeNumberExistence(Integer.parseUnsignedInt(jTextField5.getText())) || !checkDocumentClientNumberExistence(Integer.parseUnsignedInt(jTextField3.getText()))){
JOptionPane.showConfirmDialog(null, "ورودي شماره مشتري يا شماره پرسنلي ناموجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (jComboBox2.getSelectedIndex()==-1){
JOptionPane.showConfirmDialog(null, "كد معين انتخاب نشده است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (jComboBox1.getSelectedIndex()==-1 && jTextField6.getText().equals("") ){
JOptionPane.showConfirmDialog(null, "شرح انتخاب نشده است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else {
boolean exists = data_base.selectQueryCheckDocument("Select * From document;",Integer.parseUnsignedInt(jTextField1.getText()), fd_document_type, fd_document_status, jTextField2.getText(), Integer.parseUnsignedInt(jTextField3.getText()));
if (exists) {
JOptionPane.showConfirmDialog(null, "ورودي موجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else {
jButton2.setEnabled(true);
jButton3.setEnabled(true);
jTextField1.setEnabled(false);
jTextField2.setEnabled(false);
jTextField3.setEnabled(false);
jButton1.setEnabled(false);
fd_document_number = Integer.parseUnsignedInt(jTextField1.getText());
fd_document_date = jTextField2.getText();
fd_document_statement = jComboBox1.getSelectedItem().toString();
fd_document_code_number = getCodeNumber(jComboBox2.getSelectedItem().toString());
fd_document_employee_number = Integer.parseUnsignedInt(jTextField5.getText());
fd_document_client_number = Integer.parseUnsignedInt(jTextField3.getText());
document_amount = Integer.parseUnsignedInt(jTextField4.getText());
***here is the object creation: financial_document = new Document(fd_document_number, fd_document_type, fd_document_status, fd_document_date, fd_document_statement, fd_document_code_number, fd_document_employee_number, fd_document_client_number, document_amount);
financial_document.printDocumentInformation();
}
}
}
现在,我需要删除对象并创建新对象的下一个方法:
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
*** deleting: financial_document = null;
if (bill_documentType==1){
fd_document_type = true;
}
else if (bill_documentType==0){
fd_document_type = false;
}
else {
System.out.println("Error in Document Type!!!!!!!!!");
}
if (bill_documentStatus==1){
fd_document_status = true;
}
else if (bill_documentStatus==0){
fd_document_status = false;
}
else {
System.out.println("Error in Document Status!!!!!!!!!");
}
if (jTextField4.getText().equals("")|| jTextField5.getText().equals("")|| bill_documentStatus==-1){
JOptionPane.showConfirmDialog(null, "لطفا همه فيلد ها را پر كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentAmountText(jTextField4.getText())){
JOptionPane.showConfirmDialog(null, "ورودي مبلغ نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentEmployeeNumberText(jTextField5.getText())) {
JOptionPane.showConfirmDialog(null, "ورودي شماره پرسنلي نامعتبر است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (!checkDocumentEmployeeNumberExistence(Integer.parseUnsignedInt(jTextField5.getText())) || !checkDocumentClientNumberExistence(Integer.parseUnsignedInt(jTextField3.getText()))){
JOptionPane.showConfirmDialog(null, "ورودي شماره مشتري يا شماره پرسنلي ناموجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (jComboBox2.getSelectedIndex()==-1){
JOptionPane.showConfirmDialog(null, "كد معين انتخاب نشده است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else if (jComboBox1.getSelectedIndex()==-1 && jTextField6.getText().equals("") ){
JOptionPane.showConfirmDialog(null, "شرح انتخاب نشده است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else {
boolean exists = data_base.selectQueryCheckDocument("Select * From document;",Integer.parseUnsignedInt(jTextField1.getText()), fd_document_type, fd_document_status, jTextField2.getText(), Integer.parseUnsignedInt(jTextField3.getText()));
if (exists) {
JOptionPane.showConfirmDialog(null, "ورودي موجود است لطفا دوباره بررسي كنيد", "Message", JOptionPane.PLAIN_MESSAGE);
}
else {
fd_document_number = Integer.parseUnsignedInt(jTextField1.getText());
fd_document_date = jTextField2.getText();
fd_document_statement = jComboBox1.getSelectedItem().toString();
fd_document_code_number = getCodeNumber(jComboBox2.getSelectedItem().toString());
fd_document_employee_number = Integer.parseUnsignedInt(jTextField5.getText());
fd_document_client_number = Integer.parseUnsignedInt(jTextField3.getText());
document_amount = Integer.parseUnsignedInt(jTextField4.getText());
***New object creation: financial_document = new Document(fd_document_number, fd_document_type, fd_document_status, fd_document_date, fd_document_statement, fd_document_code_number, fd_document_employee_number, fd_document_client_number, document_amount);
financial_document.printDocumentInformation();
}
}
}
我想知道financial\u document=null使用该技巧,代码将正常工作。如果设置
financial\u document=null
,那么,如果应用程序中没有其他对它的引用,它将在下一次“垃圾收集”期间从内存中删除。在下一次垃圾收集之前,它基本上会被删除,但理论上,您仍然可以通过转储运行应用程序的机器的内存来访问它。通过将字段设置为private
而不是默认的访问说明符,可以帮助防止出现对它的其他引用。如果设置financial\u document=null
,那么,如果应用程序中没有对它的其他引用,它将在下一次“垃圾收集”期间从内存中删除。在下一次垃圾收集之前,它基本上会被删除,但理论上,您仍然可以通过转储运行应用程序的机器的内存来访问它。通过将字段设置为private
而不是默认的访问说明符,可以帮助防止出现其他引用。问题不在于创建/删除对象,而是问问自己:“谁知道这个对象?”
如果你知道
MyClass x = new MyClass();
…然后创建一个类型为“MyClass”的新对象,并将其引用存储在变量x中。因此,无论在中定义了什么x,它都“知道”您新创建的MyClass对象。如果你现在这样做
x = null;
…然后对“旧”MyClass对象的引用将被“遗忘”/“覆盖为null
。如果您没有将x传递到其他地方,则没有其他对象会“知道”x,因此x实际上是“丢失”的,并将被垃圾收集器从内存中完全删除
同样的情况是您覆盖了x:
x = new MyClass();
同样,之前存储在“x”中的旧对象将被“遗忘”
由于在类外声明financial\u document
后,代码就不起作用了,因此很难猜测它是否起作用,但如果用新文档替换对旧文档的引用,理论上,这应该是从内存中“删除”旧对象,是的
编辑:当您删除对财务文档的引用时,它实际上应该是从内存中“删除”您的旧对象,是的,除非您同时将其传递给外部。问题不在于创建/删除对象,而在于问自己:“谁知道这个对象?” 如果你知道
MyClass x = new MyClass();
…然后创建一个类型为“MyClass”的新对象,并将其引用存储在变量x中。因此,无论在中定义了什么x,它都“知道”您新创建的MyClass对象。如果你现在这样做
x = null;
…然后对“旧”MyClass对象的引用将被“遗忘”/“覆盖为null
。如果您没有将x传递到其他地方,则没有其他对象会“知道”x,因此x实际上是“丢失”的,并将被垃圾收集器从内存中完全删除
同样的情况是您覆盖了x:
x = new MyClass();
同样,之前存储在“x”中的旧对象将被“遗忘”
由于在类外声明financial\u document
后,代码就不起作用了,因此很难猜测它是否起作用,但如果用新文档替换对旧文档的引用,理论上,这应该是从内存中“删除”旧对象,是的
编辑:当你删除对你的财务文档的引用时,它实际上应该是从内存中“删除”你的旧对象,是的,除非你同时将它传递给外部。我认为财务文档是类中的一个字段-他们只是没有发布整个类,只是其中的片段。啊,你是对的,我的错,它只是在构造函数之外,而不是在类之外。我确实读错了。有没有其他方法来删除和创建?我认为financial_文档是类上的一个字段-他们只是没有发布整个类,只是其中的片段。啊,你是对的,我的错,它只是在构造函数之外,而不是在类之外。我确实读错了。有没有其他方法可以删除和创建?有没有其他方法可以删除和创建?有没有其他方法可以删除和创建?有没有其他方法可以删除和创建?有没有其他方法可以删除和创建?