Java: What is the purpose of creating an object on the heap without a link

The code I came across was like this: an object is created and its method is 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 item be destroyed by the clothing collection after request ()?
  • What is the advantage of creating an object on a heap without a link, as in this case?
+1
source share
6 answers

The point is just to call the request method.

And yes, as soon as the instructions end, the object can be garbage.

Please note that in this code, since the object is initialized without parameters, if there are no other constructors or methods, it seems that the method should be made static: the instance seems completely useless.

+5
source

Will the object be destroyed by the collection of clothes after calling the () method?

Yes, it will be GCd, as soon as the main method ends, the scope of the object is the main method of brackets

Why do we create an object without a link?

request() is a non-static method, and we need an instance of the DemoSoap class to call this method. And if the code just calls the method and does nothing with the created pf DemoSoap , then it makes no sense to have a link.

+2
source
 new DemoSoap().request(); 

A DemoSoap object is DemoSoap and its request() method is called, and the object is entitled to GC. The only purpose of creating an instance of this object was to call request() once, so there is no need to store a link to this object in the future. In your particular case, I do not think it matters a lot.

But an unnecessary reference to an object that you do not need in the future can be bad. Let's pretend that

  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 . } 

PS: I learned that it’s good practice to release links as quickly as reasonably possible to release.

+1
source

Yes, the object will collect grbage after the area ( main() method) is left.

Also, in your case, the full program will end, so there is no longer any need to process objects after exiting the program.

0
source

In your case, the point is the call to the request() method. You simply cannot call it without an instance of the object.

In the general case, even a constructor can have useful side effects (although this is certainly not a recommended design), so you can name it even if you never use this instance at all.

for instance

  new JdbcDriver(); 

registers the database driver in the global JDBC registry, so that it understands the connection strings for this driver (which would not be done before you register the driver with this trick or more "correctly").

0
source

sometimes you no longer need an object, you just want to call the method once. Sometimes you need to create an object that controls another object that works like a stream

0
source

All Articles