The code I came across was this, an object is created and it’s method was called:
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); }
}
- Will the object be destroyed by garbace collection after the
request() method?
- what is the advantage of creating an object in the heap with no reference, as in this case?
The point is simply to be able to call the request
method.
And yes, as soon as the statements ends, the object can be garbaged.
Note that in this code, as the object is initialized without parameters, unless there are other constructors or methods, it seems the method should have been made static : the instance seems totally useless.
Will the object be destroyed by garbace collection after the request()
method?
Yes it will be GCd as soon as main method ends, the scope of the object is the main method's braces
Why we create an object without a reference?
request()
is a non-static method and we need an instance of the class DemoSoap
to invoke that method. And if a code is just about invoking the method and doing nothing else with the created instance pf DemoSoap
, then there is no point in having reference.
new DemoSoap().request();
An object of DemoSoap
is created and its method request()
is invoked and the object is eligible for GC . The only purpose of instantiating that object was a one time call to request()
, hence no need to keep a reference to that object for future. In your particular case I don't think it makes much difference.
But unecessary holding reference to an object which you don't need in future can be bad . Suppose ,
void somemethod() {
Demo demo = new Demo();
demo.request(); // purpose of demo ends here .
.......
........
lots of processing in between
.......
// demo still refers to an object in heap uselessly .
}
P.S.: I have learnt that it is a good practice to release references as quickly as they can reasonably be released.
Yes the object will be grbage collected after the scope (the main()
method) is left.
Also in your case the complete program will end therefore there is no more need to handle objects after quiting the program.
In your case, the point is to call the request()
method. You just cannot call it without an object instance.
In the general case, even a constructor can have useful side-effects (though that is certainly not an encouraged design choice), so you might want to call it even if you never use the instance at all.
For example
new JdbcDriver();
registers the database driver into JDBC's global registry, so that it understands the connection strings for that driver (which it would not before you registered the driver by this trick or more "proper" means).
sometimes you don't need the object anymore, you just want to call a method once. Sometimes you need to create an object that instruments another object that runs as a thread