我遇到的代码是这样的,创建了一个对象并调用了它的方法:
public static void main(String[] args) {
new DemoSoap().request(); //<----how come there is no reference?
}
private void request() {
try {
// Build a SOAP message to send to an output stream.
SOAPMessage msg = create_soap_message();
// Inject the appropriate information into the message.
// In this case, only the (optional) message header is used
// and the body is empty.
SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
SOAPHeader hdr = env.getHeader();
// Add an element to the SOAP header.
Name lookup_name = create_qname(msg);
hdr.addHeaderElement(lookup_name).addTextNode("time_request");
// Simulate sending the SOAP message to a remote system by
// writing it to a ByteArrayOutputStream.
out = new ByteArrayOutputStream();
msg.writeTo(out);
trace("The sent SOAP message:", msg);
SOAPMessage response = process_request();
extract_contents_and_print(response);
}
catch(SOAPException e) { System.err.println(e); }
catch(IOException e) { System.err.println(e); }
}
- request() 方法后对象会被垃圾回收销毁吗?
- 在这种情况下,在没有引用的情况下在堆中创建对象有什么好处?