Java:在没有参考的情况下在堆中创建对象的目的是什么 [英] Java: What is the purpose of creating an object in the heap with no reference

查看:195
本文介绍了Java:在没有参考的情况下在堆中创建对象的目的是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的代码是这样的,一个对象被创建,它的方法被调用:

  public static void main String [] args){
DemoSoap()。request(); //< ----怎么没有参考?




private void request(){
try {
//构建一个SOAP消息发送到输出流。
SOAPMessage msg = create_soap_message();

//在消息中注入适当的信息。
//在这种情况下,只有(可选)消息头被使用
//并且主体为空。
SOAPEnvelope env = msg.getSOAPPart()。getEnvelope();
SOAPHeader hdr = env.getHeader();

//向SOAP头添加一个元素。
名称lookup_name = create_qname(msg);
hdr.addHeaderElement(lookup_name).addTextNode(time_request);

//模拟将SOAP消息发送到远程系统
//将其写入ByteArrayOutputStream。
out = new ByteArrayOutputStream();
msg.writeTo(out);

trace(发送的SOAP消息:,msg);

SOAPMessage response = process_request();
extract_contents_and_print(response);
}
catch(SOAPException e){System.err.println(e); }
catch(IOException e){System.err.println(e); }
}




  • Garbace Collection会销毁对象在
    request()方法之后?

  • 在这个例子中,在堆中创建一个对象的好处是什么?


解决方案

关键是简单地调用请求方法。

是的,一旦语句结束,对象可以被垃圾清理。



请注意,在这个代码,因为对象被初始化时没有参数,除非有其他的构造函数或方法,否则它的方法应该是静态的:实例看起来完全没用。


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.

这篇关于Java:在没有参考的情况下在堆中创建对象的目的是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆